Android用复用/重用布局
16lz
2021-01-26
Android用用<include>复用/重用布局
用<include>嵌入其他布局文件
<LinearLayout xmlns:android ="http://schemas.android.com/apk/res/android" xmlns:tools= "http://schemas.android.com/tools" android:layout_width = "match_parent" android:layout_height= "match_parent" android:paddingBottom= "@dimen/activity_vertical_margin" android:paddingLeft= "@dimen/activity_horizontal_margin" android:paddingRight= "@dimen/activity_horizontal_margin" android:paddingTop= "@dimen/activity_vertical_margin" tools:context = ".MainActivity" android:orientation = "vertical" > <include layout= "@layout/titlebar"> <TextView android:layout_width ="wrap_content" android:layout_height ="wrap_content" android:text ="@string/hello_world" > < LinearLayout>你也可以在当前布局中复写重用组件的android:layout_*属性
<include android:id ="@+id/news_title" android:layout_width ="match_parent" android:layout_height ="match_parent" layout= "@layout/titlebar" >
使用<merge>标签作为根,
<merge/>标签可以帮助你在你的视图层次中消除多余的视图组,当一个布局包含另外一个布局时。当你的主布局是一个垂直结构的LinearLayout包含两个连续的能被其它布局重用的视图,被你放置在布局中的两个可重用的视图都需要各自的根视图,使用另外一个LinearLayout来充当可重用视图的根视图时,会导致一个垂直结构的LinearLayout嵌套在另外一个垂直结构的LinearLayout中,嵌套的LinearLayout除了减慢你的UI渲染速度以外没有任何的实际作用,为了避免这种情况的发生,我们可以使用<merge/>标签来作为可重用布局组件的根视图;
<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>同样是使用<include/>标签来添加进布局中,这样Android系统并不会理会<merge/>标签,而是直接把两个Button放置在布局中,避免了不必要的嵌套。另外需要注意的是<merge/>只可以作为布局的根节点,当需要包含其它布局组件的布局本身是以<merge/>为根节点的话,需要将被导入的xmllayout置于viewGroup中,同时需要设置attachToRoot为True
更多相关文章
- Android中常用的五种布局方式:LinearLayout
- Android竖屏模式实现横屏效果
- android:创建自定义控件
- Android软键盘(二) 软键盘弹出布局上移的问题
- Android进阶练习-自定义视图(3)
- Android(安卓)增强版百分比布局库 Eclipse Demo
- Android——layout_marginStart和layout_marginEnd的详细讲解
- 超级简单的android通过view自带的方法进行裁剪视图,实现圆形,矩形
- Android(安卓)listview的适配器以及各种监听、效率的提升