1 StubView
作用:StubView标签中的布局只有在需要的时候才会被渲染加载。
注意:StubView的渲染加载操作只能执行一次;不支持合并标签
使用示例:
(1) ViewStub中引用的布局
& lt; ? xml version=" 1.0 ",编码=" utf - 8 " ?比; LinearLayout & lt; ,,,android: id=癅 + id/stublayout” ,,,xmlns: android=" http://schemas.android.com/apk/res/android " ,,,android:取向=按怪薄?android: layout_width=癿atch_parent” ,,,android: layout_height=癿atch_parent”比; ,,,& lt; TextView ,,,,,,,android: id=癅 + id/sbtv” ,,,,,,,android: layout_width=?00 dp” ,,,,,,,android: layout_height=?00 dp” ,,,,,,,android:文本=比?能够is sub text 视图”/比; & lt;/LinearLayout>
(2)使用ViewStub
& lt; ViewStub ,,,android: id=癅 + id/viewstub” ,,,android: layout_width=?00 dp” ,,,android: layout_height=?00 dp” ,,,android:布局=癅layout/sublayout”/祝辞
(3) java代码中渲染加载
ViewStub stub =, (ViewStub) findViewById (R.id.viewstub); stub.inflate (); TextView stubtv =, (TextView) layout.findViewById (R.id.sbtv); stubtv.setText (“hello 存根!”);
2包括标签
作用:将引用的布局替换到当前布局中该标签所处的位置;
注意:引用的布局非合并,设置inlude的id属性后会覆盖掉引用布局顶层布局的id;
示例1引用布局非合并
(1)引用布局
& lt; ? xml version=" 1.0 ",编码=" utf - 8 " ?比; & lt; LinearLayout  xmlns: android=" http://schemas.android.com/apk/res/android " ,,,android: id=癅 + id/sharedlayout2” ,,,android: layout_width=?00 dp” ,,,android: layout_height=?00 dp” ,,,android:背景=癅android:颜色/黑”比; ,,,& lt; TextView ,,,,,,,android: id=癅 + id/mytv” ,,,,,,,android: layout_width=?00 dp” ,,,,,,,android: layout_height=?00 dp” ,,,,,,,android:背景=癅android:颜色/holo_blue_light” ,,,,,,,android:文本=罢馐币桓龉玫牟季帧?比; & lt;/LinearLayout>
(2)使用包括标签
& lt;包括 ,,,android: id=癅 + id/include1”& lt; !——该id会覆盖布局(1)中LinearLayout的id;——比; ,,,布局=癅layout/我” ,,,android: layout_width=?0 dp” ,,,android: layout_height=?0 dp祝辞& lt;/include>
(3) java中使用
LinearLayout sharedlayout =, (LinearLayout), findViewById (R.id.include1); TextView tv =, (TextView), sharedlayout.findViewById (R.id.mytv); tv.setText (“hello include1”);
示例2引用合并布局
(1)引用布局
& lt; merge xmlns: android=" http://schemas.android.com/apk/res/android "比; ,,,& lt; LinearLayout ,,,,,,,android: id=癅 + id/mergetlayout” ,,,,,,,android: layout_width=皐rap_content” ,,,,,,,android: layout_height=皐rap_content”比; ,,,,,,,& lt; TextView ,,,,,,,,,,,android: id=癅 + id/mergetv1” ,,,,,,,,,,,android: layout_width=40 dp” ,,,,,,,,,,,android: layout_height=40 dp” ,,,,,,,,,,,android:背景=癅android:颜色/holo_blue_bright” ,,,,,,,,,,,android:文本=癿erge text1”,/比; ,,,,,,,& lt; TextView ,,,,,,,,,,,android: id=癅 + id/mergetv2” ,,,,,,,,,,,android: layout_width=40 dp” ,,,,,,,,,,,android: layout_height=40 dp” ,,,,,,,,,,,android:背景=癅android:颜色/darker_gray” ,,,,,,,,,,,android:文本=癿erge text2”,/比; ,,,& lt;/LinearLayout> & lt;/merge>
(2)包括标签:合并布局没有id属性,所以这里的id其实没有意义
& lt;包括 ,,,android: id=癅 + id/include2” null null null null null null null null null null null null null null null null null null null null null null null null null nullAndroid布局优化——ViewStub,包括合并