android xml布局之———include.merge.ViewStub
装载:张兴业 http://blog.csdn.net/xyz_lmn
在布局优化中,Androi的官方提到了这三种布局
1、布局重用
[html] view plain copy print ?
- <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>
1)
2)可以使用其他属性。
3)在include标签中所有的android:layout_*都是有效的,前提是必须要写layout_width和layout_height两个属性。
4)布局中可以包含两个相同的include标签,引用时可以使用如下方法解决(参考):
[html] view plain copy print ?
- View bookmarks_container_2 = findViewById(R.id.bookmarks_favourite);
- bookmarks_container_2.findViewById(R.id.bookmarks_list);
2、减少视图层级
[html] view plain copy print ?
- <merge xmlns:android="http://schemas.android.com/apk/res/android">
- <Button
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="@string/add"/>
- <Button
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="@string/delete"/>
- merge>
现在,当你添加该布局文件时(使用
3、需要时使用
[html] view plain copy print ?
- <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" />
当你想加载布局时,可以使用下面其中一种方法:
- ((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目前有个缺陷就是还不支持
更多
更多相关文章
- Android(安卓)UI开发第四十一篇——墨迹天气3.0引导界面及动画实
- Android(安卓)样式的开发
- 2.2.3帧布局
- Android线性布局与表格布局
- Android软键盘弹出将底部栏顶上去并不会挤压界面
- Android(安卓)切换卡(TabWidget)
- Android虚拟导航栏遮挡底部的输入框的解决方法
- 详解Android中获取SD卡和内存的空间信息
- [置顶] Android(安卓)中轴时光轴