`

Android抽象布局——include、merge 、ViewStub

 
阅读更多
引用
http://blog.csdn.net/xyz_lmn/article/details/14524567


1、布局重用<include />
<include />标签能够重用布局文件,简单的使用如下:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width=”match_parent”
    android:layout_height=”match_parent”
    android:background="@color/app_bg"
    android:gravity="center_horizontal">
   
    <include layout="@layout/titlebar"/>
   
    <TextView android:layout_width=”match_parent”
              android:layout_height="wrap_content"
              android:text="@string/hello"
              android:padding="10dp" />
    ... 
</LinearLayout>


[color=green][size=large]1)<include />标签可以使用单独的layout属性,这个也是必须使用的。
2)可以使用其他属性。<include />标签若指定了ID属性,而你的layout也定义了ID,则你的layout的ID会被覆盖,解决方案。
3)在include标签中所有的android:layout_*都是有效的,前提是必须要写layout_width和layout_height两个属性。
4)布局中可以包含两个相同的include标签,引用时可以使用如下方法解决(参考):
View bookmarks_container_2 = findViewById(R.id.bookmarks_favourite); 
bookmarks_container_2.findViewById(R.id.bookmarks_list);[/size][/color]




2、需要时使用<ViewStub />
   <ViewStub />标签最大的优点是当你需要时才会加载,使用他并不会影响UI初始化时的性能。各种不常用的布局想进度条、显示错误消息等可以使用<ViewStub />标签,以减少内存使用量,加快渲染速度。<ViewStub />是一个不可见的,大小为0的View。<ViewStub />标签使用如下:
<ViewStub  
    android:id="@+id/stub_import"  
    android:inflatedId="@+id/panel_import"  
    android:layout="@layout/progress_overlay"  
    android:layout_width="fill_parent"  
    android:layout_height="wrap_content"  
    android:layout_gravity="bottom" />  


当你想加载布局时,可以使用下面其中一种方法:

[java] view plaincopyprint?在CODE上查看代码片派生到我的代码片
((ViewStub) findViewById(R.id.stub_import)).setVisibility(View.VISIBLE); 
// or 
View importPanel = ((ViewStub) findViewById(R.id.stub_import)).inflate(); 

当调用inflate()函数的时候,ViewStub被引用的资源替代,并且返回引用的view。 这样程序可以直接得到引用的view而不用再次调用函数findViewById()来查找了。
注:ViewStub目前有个缺陷就是还不支持 <merge /> 标签。
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics