recycle ui elements
hi to all in my activity layout i use this XML to get an array of buttons
<TextView
android:id="@+id/tab1"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:text="text tab 1"
android:gravity="center"
android:padding="10dp"
android:clickable="true"
android:background="@drawable/tab_color_selector"
/>
<View
android:layout_width="1dp"
android:layout_height="fill_parent"
android:background="@color/lightgrey"
/>
<TextView
android:id="@+id/tab2"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:text="text tab 2"
android:gravity="center"
android:padding="10dp"
android:clickable="true"
android:background="@drawable/tab_color_selector"
/>
<View
android:layout_width="1dp"
android:layout_height="fill_parent"
android:background="@color/lightgrey"
/>
<TextView
android:id="@+id/tab3"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:text="text tab 3"
android:gravity="center"
android:padding="10dp"
android:clickable="true"
android:background="@drawable/tab_color_selector"
/>
<View
android:layout_width="1dp"
android:layout_height="fill_parent"
a开发者_如何学运维ndroid:background="@color/lightgrey"
/>
Considering this copy/paste very ugly, i've created a new xml file named 'tab.xml' and defined as follow
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:orientation="horizontal" >
<TextView
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:gravity="center"
android:padding="10dp"
android:clickable="true"
android:background="@drawable/tab_color_selector"
/>
<View
android:layout_width="1dp"
android:layout_height="fill_parent"
android:background="@color/lightgrey"
/>
</LinearLayout>
Returning to the primary layout, i've substitued the first code with three...
<include layout="@layout/tab" />
...but now, how can i set the textview text? Trying
<include android:text="text tab 1" layout="@layout/tab" />
doesn't works: still no text...
Thanks in advance L.
What you're looking for is a fragment. Use Fragments API as it is available for all android platforms since 1.6 through SDK Updater (SDK Setup) with label «Android Compatibility package».
精彩评论