Re-use Layouts with include

This example show how to use layout defined in seperated XML, using <include>. Such that you can re-use common view in your layout, or in separated layout.

Re-use Layouts with <include/>
The same layour included twice using <include>
Create the re-use layout, /res/layout/titlebar.xml.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:background="@android:color/background_dark">

<ImageView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher" />

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Android-Coding"
android:textColor="@android:color/white"
android:textStyle="bold"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="http://lesapplication.blogspot.com/"
android:textColor="@android:color/white"
android:textStyle="italic"/>

</LinearLayout>
</LinearLayout>

Include titlebar.xml in our main layout:
<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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >

<include layout="@layout/titlebar"
android:id="@+id/topbanner"
android:layout_alignParentTop="true"/>

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/topbanner"
android:text="@string/hello_world" />

<include layout="@layout/titlebar"
android:id="@+id/topbanner2"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_alignParentBottom="true"/>
</RelativeLayout>


0 Response to "Re-use Layouts with include"

Posting Komentar