Android 的原生的抽象布局一共有3个 include ViewStub merge ,每一个都很强大,让我们在实际开发中省去不少时间
< include> 标签 一个布局重用的标签 在material design 的设计风格中,每一个activity 都会有一个风格一样的title ,如果每一个xml文件你都要写一边title的布局,不仅浪费时间,修改起来还非常麻烦,每一个文件都要修改,简单的使用方法
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#693">
<ImageView
android:id="@+id/iv_include"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_gravity="center_vertical"/>
<TextView
android:id="@+id/tv_include"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="这是标题"
android:layout_gravity="center"
/>
</FrameLayout>
自己随便写了一个布局文件 ,看做是一个ActionBar ,在同一个app中所欲的activity都要,
<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:orientation="vertical">
<include
layout="@layout/item_title_include"/>
</LinearLayout>
在这里的include正需要一个layout的属性 如果给他添加了id属性,子布局的id节点就变了
ImageView iv_include = (ImageView)findViewById(R.id.iv_include);
TextView tv_include = (TextView)findViewById(R.id.tv_include);
iv_include.setBackgroundResource(R.drawable.icon_64);
tv_include.setText("注册2");
在布局使用时直接findViewById就可以了,和普通的view没有区别
<viewstub>标签 ,这种标签常常和ListView这种控件使用, viewstub 标签在整个布局加载过程中并没有infalte ,如果想要强制加载viewstub标签,必须调用
stubview.inflate()的方法,非常适合做ListView的emptyview ,只有在listview的数据为空时才显示,其他时候隐藏
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/empty_tv"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textColor="#000"
android:textSize="20sp"
android:text="未找到图片资源"
android:gravity="center"
/>
简单的一个textview 设置了一下字体和颜色,
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<GridView
android:id="@+id/gv_photo"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:verticalSpacing="10dp"
android:horizontalSpacing="10dp"
android:cacheColorHint="#00000000"
android:listSelector="#00000000"
android:fadeScrollbars="true"
android:numColumns="2"
android:background="#fff"></GridView>
<ViewStub
android:id="@+id/vs"
android:layout_width="match_parent"
android:layout_height="match_parent"
<span style="white-space:pre"> </span>android:inflateId="@+id/inflate_id"
android:layout="@layout/empty_photo"/>
</FrameLayout>
这里我使用的GridView 其实效果是一样的
ViewStub vs=(ViewStub)findViewById(R.id.vs);
vs.inflate();
<span style="white-space:pre"> </span>View view=findViewById(R.id.inflate_id);
TextView empty_tv = (TextView)view.findViewById(R.id.empty_tv);
empty_tv.setOnClickListener(this);
empty_tv.setText("未找到图片资源");
<span style="white-space:pre"> </span>listview.setEmptyView(view);
这里面需要注意的是 在viewstub 使用的过程中,如果需要为其中的控件设置点击事件必须要inflate(), 而且
还需要在xml中配置inflateId ,在调用inflate()之后,按照inflateId找到这个被inflate控件就可以随便使用了
< merge> 标签 减少视图图层, 布局顶结点是FrameLayout且不需要设置background或padding等属性,可以用merge代替,因为Activity内容试图的parent view就是个FrameLayout,所以可以用merge消除只剩一个。在使用include时,使用merge当作该布局的顶节点,这样在被引入时顶结点会自动被忽略,而将其子节点全部合并到主布局中。