Making your Layout flexible
In the above code we sort of hard coded everything in the main layout of the activity. But you may want to inflate the contents of each layout dynamically in your code. Or, more importantly you may want to make this layout reusable to your other activities, each one having their own contents to display and may be their own headers and footers.
To achieve that, we are simply going to separate every component from the other. We are going to create the header layout to a different XML file and do the same for the footer. Now, every activity that has its own content can include the header and the footer in their own Layouts.
To create a new XML Layout file containing the items you want to fill your with. To create a new layout file, go to the Package Explorer and find folder. Right Click on the folder -> New -> Other -> Android -> Android XML Layout file:
Put a name for your new layout file, and select RelativeLayout from the list (although this is not absolutely necessary) and click “Finish”:
You need to create also.So,after the creation of the new layout XML files, this is the new Project Structure:
header.xml
<RelativeLayout
android:id="@+id/footer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="#FC0"
android:gravity="center" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:text="Fixed Footer"
android:textColor="#000"
android:textSize="20sp" />
</RelativeLayout>
footer.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/footer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="#FC0"
android:gravity="center" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:text="Fixed Footer"
android:textColor="#000"
android:textSize="20sp" />
</RelativeLayout>
main.xml
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" > <!-- Header aligned to top --> <include layout="@layout/header" /> <!-- Footer aligned to bottom --> <include layout="@layout/footer" /> <!-- Content below header and above footer --> <RelativeLayout android:id="@+id/content" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_above="@id/footer" android:layout_below="@id/header" android:gravity="center" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Content" android:textColor="#33E" android:textSize="20sp" /> </RelativeLayout> </RelativeLayout>
As you can see we’ve simply separated the RelativeLayouts of the header and the footer to different XML files. So now, you can include them in any Layout you want. You can do that by writtingto include the header and ( and must correspond to the names you gave to the respective XML Layout files)
Run the application
This is how our Layout looks on the emulator:
No comments:
Post a Comment