1 StubViewjava
做用:StubView標籤中的佈局只有在須要的時候纔會被渲染加載。android
注意:StubView的渲染加載操做只能執行一次;不支持merge標籤ide
使用示例:佈局
(1)ViewStub中引用的佈局this
<?xml version="1.0" encoding="utf-8"?> <LinearLayout android:id="@+id/stublayout" xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:id="@+id/sbtv" android:layout_width="300dp" android:layout_height="300dp" android:text="this is sub text view"/> </LinearLayout>
(2)使用ViewStubxml
<ViewStub android:id="@+id/viewstub" android:layout_width="100dp" android:layout_height="100dp" android:layout="@layout/sublayout"/>
(3)java代碼中渲染加載utf-8
ViewStub stub = (ViewStub)findViewById(R.id.viewstub); stub.inflate(); TextView stubtv = (TextView)layout.findViewById(R.id.sbtv); stubtv.setText("hello stub!");
2 include標籤get
做用:將引用的佈局替換到當前佈局中該標籤所處的位置;it
注意:引用的佈局非merge,設置inlude的id屬性後會覆蓋掉引用佈局頂層layout的id;io
示例1 引用佈局非merge
(1)引用佈局
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/sharedlayout2" android:layout_width="200dp" android:layout_height="200dp" android:background="@android:color/black"> <TextView android:id="@+id/mytv" android:layout_width="100dp" android:layout_height="100dp" android:background="@android:color/holo_blue_light" android:text="這時一個公用的佈局"/> </LinearLayout>
(2)使用include標籤
<include android:id="@+id/include1"<!--該id會覆蓋佈局(1)中LinearLayout的id;--> layout="@layout/my" android:layout_width="50dp" android:layout_height="50dp"></include>
(3)java中使用
LinearLayout sharedlayout = (LinearLayout) findViewById(R.id.include1); TextView tv = (TextView) sharedlayout.findViewById(R.id.mytv); tv.setText("hello include1");
示例2 引用merge佈局
(1)引用佈局
<merge xmlns:android="http://schemas.android.com/apk/res/android"> <LinearLayout android:id="@+id/mergetlayout" android:layout_width="wrap_content" android:layout_height="wrap_content"> <TextView android:id="@+id/mergetv1" android:layout_width="40dp" android:layout_height="40dp" android:background="@android:color/holo_blue_bright" android:text="merge text1" /> <TextView android:id="@+id/mergetv2" android:layout_width="40dp" android:layout_height="40dp" android:background="@android:color/darker_gray" android:text="merge text2" /> </LinearLayout> </merge>
(2)include標籤:merge佈局沒有id屬性,因此這裏的id其實沒有意義
<include android:id="@+id/include2" layout="@layout/mymerge"></include>
(3)java中使用
//LinearLayout layout = (LinearLayout)findViewById(R.id.include2);//經過該代碼沒法獲取(1) 中的LinearLayout,這裏的layout爲null TextView mTV1 = (TextView)findViewById(R.id.mergetv1); mTV1.setText("hello merge tv1"); TextView mtv2 = (TextView)findViewById(R.id.mergetv2); mtv2.setText("hello merge tv2");
3 merge標籤
merge標籤至關於控件的簡單集合,被include到其餘佈局中後根據所處容器佈局結合各個控件的相關屬性進行佈局。
注意:merge標籤沒有id屬性
使用示例:
(1)merge佈局
<merge xmlns:android="http://schemas.android.com/apk/res/android"> <TextView android:id="@+id/mergetv1" android:layout_width="40dp" android:layout_height="40dp" android:background="@android:color/holo_blue_bright" android:text="merge text1" /> <TextView android:id="@+id/mergetv2" android:layout_width="40dp" android:layout_height="40dp" android:background="@android:color/darker_gray" android:text="merge text2" /> </merge>
此時的佈局爲兩個textview重疊而且mergetv2處於上層;
(2)merge標籤使用
<include android:id="@+id/include2" layout="@layout/mymerge"></include>
(3)因爲該include標籤處於一個豎直的linearlayout中,此時merge佈局的顯示效果以下:
merge text 1 |
merge text 2 |
TextView mTV1 = (TextView)findViewById(R.id.mergetv1); mTV1.setText("hello merge tv1"); TextView mtv2 = (TextView)findViewById(R.id.mergetv2); mtv2.setText("hello merge tv2");