Android布局优化:include
、merge
、viewstub
使用区别
一 include
标签的使用:
文件1:titlebar.xml
文件
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<Button
android:id="@+id/back"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:text="返回按钮" />
<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="提示文字"
android:textSize="20sp" />
<Button
android:id="@+id/close"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:text="关闭按钮" />
</RelativeLayout>
文件2:activity_main.xml
<RelativeLayout 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"" >
<!--引入titlebar页面-->
<include
android:layout_width="match_parent"
android:layout_height="40dp"
layout="@layout/titlebar" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:onClick="click"
android:text="按钮显示 />
</RelativeLayout>
include
标签使用注意点:
<include>
标签当中,可以重写所有layout
属性的,如上面include
中指定的layout
属性将会覆盖掉titlebar
中指定的layout
属性。
而非layout
属性则无法在<include>
标签当中进行覆写。另外需要注意的是,如果我们想要在<include>
标签当中覆写layout
属性,
必须要将layout_width
和layout_height
这两个属性也进行覆写,否则覆写效果将不会生效- 一个
xml
布局文件有多个include
标签需要设置ID
,才能找到相应子View
的控件,否则只能找到第一个include
的layout
布局,以及该布局的控件。 - 如果我们给
include
所加载的layout
布局的根容器设置了id
属性,也在include
标签中设置了id
属性,同时需要在代码中获取根容器的控件对象
时,最好将这两个id
设置相同的名称!否则,可能获取不到根容器对象,即为null
。
二 merge标签的使用
文件1:login.xml
<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:text="登录按钮" />
</merge>
文件2:activity_login.xml
<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"
android:background="@android:color/holo_blue_light">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="40dp"
android:hint="请输入用户名" />
<include layout="@layout/login" />
</LinearLayout>
通过使用merge
标签,会比第一种的使用include
标签减少一层viewgroup
布局容器,提升UI
渲染性能.
merge
标签使用注意点:
- 根布局是
FrameLayout
且不需要设置background
或padding
等属性,可以用merge
代替,因为Activity
的ContentView
父元素就是FrameLayout
,所以可以用merge
消除只剩一个. - 因为
merge
标签并不是View,所以在通过LayoutInflate.inflate()
方法渲染的时候,第二个参数必须指定一个父容器,且第三个参数必须为true
,也就是必须为merge
下的视图指定一个父亲节点.由于merge
不是View
所以对merge
标签设置的所有属性都是无效的. merge
标签必须使用在根布局,并且ViewStub
标签中的layout
布局不能使用merge
标签.
三 viewstub
标签的使用
文件1:otherinfo.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="wrap_content" >
<EditText
android:id="@+id/weichat_id"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="10dp"
android:hint="请输入微信号" />
<EditText
android:id="@+id/address_id"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="10dp"
android:hint="请输入家庭住址" />
</LinearLayout>
文件2:activity_main.xml
<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:background="@android:color/holo_blue_light"
android:orientation="vertical" >
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="40dp"
android:hint="请输入用户名" />
<ViewStub
android:id="@+id/viewstub"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout="@layout/otherinfo" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="show"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="10dp"
android:text="显示" />
</LinearLayout>
文件3:activity
中代码如下
public void show(){
ViewStub stub = ((ViewStub) findViewById(R.id.viewstub));
if(stub!=null){
View stubView = stub.inflate(); //inflate方法只能调用一次,第二次会抛出异常
EditText address = (EditText) stubView.findViewById(R.id.address_id);
EditText wechatId = (EditText) stubView.findViewById(R.id.weichat_id);
}
}
ViewStub
标签使用注意点:
ViewStub
标签不支持merge
标签。因此这有可能导致加载出来的布局存在着多余的嵌套结构,具体如何去取舍就要根据各自的实际情况来决定了。ViewStub
的inflate
只能被调用一次,第二次调用会抛出异常。- 虽然
ViewStub
是不占用任何空间的,但是每个布局都必须要指定layout_width
和layout_height
属性,否则运行就会报错。
关于ViewStub
主要属性以及方法说明如下:
android:layout
属性
加载包含的layout
布局文件;android:inflatedId
属性
重写包含的layout
布局文件的根容器id
;inflate()
方法
与setVisible(int)
方法作用类似,都可以使内容得以显示,只是inflate()
会返回一个View
对象,避免了额外使用findViewById()
方法获取layout
视图对象。