Android布局优化----ViewStub、include、merge
1 StubView 作用:StubView标签中的布局只有在需要的时候才会被渲染加载。 注意:StubView的渲染加载操作只能执行一次;不支持merge标签 使用示例: (1)ViewStub中引用的布局 1 2 3 4 5 6 7 8 9 10 11 12 13 14 <? 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 = "thisissubtextview" /> </ LinearLayout > (2)使用ViewStub 1 2 3 4 5 < ViewStub android:id = "@+id/viewstub" android:layout_width = "100dp" android:layout_height = "100dp" android:layout = "@layout/sublayout" /> (3)java代码中渲染加载 1 2 3 4 ViewStubstub=(ViewStub)findViewById(R.id.viewstub); stub.inflate(); TextViewstubtv=(TextView)layout.findViewById(R.id.sbtv); stubtv.setText( "hellostub!" ); 2 include标签 作用:将引用的布局替换到当前布局中该标签所处的位置; 注意:引用的布局非merge,设置inlude的id属性后会覆盖掉引用布局顶层layout的id; 示例1 引用布局非merge (1)引用布局 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 <? 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标签 1 2 3 4 5 <include android:id= "@+id/include1" <!--该id会覆盖布局( 1 )中LinearLayout的id;--> layout= "@layout/my" android:layout_width= "50dp" android:layout_height= "50dp" ></include> (3)java中使用 1 2 3 LinearLayoutsharedlayout=(LinearLayout)findViewById(R.id.include1); TextViewtv=(TextView)sharedlayout.findViewById(R.id.mytv); tv.setText( "helloinclude1" ); 示例2 引用merge布局 (1)引用布局 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 < 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 = "mergetext1" /> < TextView android:id = "@+id/mergetv2" android:layout_width = "40dp" android:layout_height = "40dp" android:background = "@android:color/darker_gray" android:text = "mergetext2" /> </ LinearLayout > </ merge > (2)include标签:merge布局没有id属性,所以这里的id其实没有意义 1 2 3 < include android:id = "@+id/include2" layout = "@layout/mymerge" ></ include > (3)java中使用 1 2 3 4 5 //LinearLayoutlayout=(LinearLayout)findViewById(R.id.include2);//通过该代码无法获取(1)中的LinearLayout,这里的layout为null TextViewmTV1=(TextView)findViewById(R.id.mergetv1); mTV1.setText( "hellomergetv1" ); TextViewmtv2=(TextView)findViewById(R.id.mergetv2); mtv2.setText( "hellomergetv2" ); 3 merge标签 merge标签相当于控件的简单集合,被include到其他布局中后根据所处容器布局结合各个控件的相关属性进行布局。 注意:merge标签没有id属性 使用示例: (1)merge布局 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 < 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 = "mergetext1" /> < TextView android:id = "@+id/mergetv2" android:layout_width = "40dp" android:layout_height = "40dp" android:background = "@android:color/darker_gray" android:text = "mergetext2" /> </ merge > 此时的布局为两个textview重叠并且mergetv2处于上层; (2)merge标签使用 1 2 3 < include android:id = "@+id/include2" layout = "@layout/mymerge" ></ include > (3)由于该include标签处于一个竖直的linearlayout中,此时merge布局的显示效果如下: merge text 1 merge text 2 (4)java中使用 1 2 3 4 TextViewmTV1=(TextView)findViewById(R.id.mergetv1); mTV1.setText( "hellomergetv1" ); TextViewmtv2=(TextView)findViewById(R.id.mergetv2); mtv2.setText( "hellomergetv2" ); 本文转自wauoen51CTO博客,原文链接:http://blog.51cto.com/7183397/1847175 ,如需转载请自行联系原作者