一、使用抽象标签
(1)<include>标签
<include>标签,是将布局的公共部分抽取出来,以实现布局的模块化,例如我们一个app顶部的布局基本是固定,所以我们可以将其抽取出来以便共用
下面就是我们的公共布局top.xml
<pre name="code" class="java"> <LinearLayout android:id="@+id/top" android:layout_width="match_parent" android:layout_height="54dp" android:background="#ffffff"> <RelativeLayout android:id="@+id/back" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1"> <ImageView android:id="@+id/img_back" android:layout_width="28dp" android:layout_height="28dp" android:background="@drawable/back" android:layout_centerVertical="true" android:layout_marginLeft="15dp"/> </RelativeLayout> <RelativeLayout android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="标题" android:textSize="19sp" android:textColor="#292d33" android:layout_centerInParent="true"/> </RelativeLayout> <RelativeLayout android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1"> </RelativeLayout> </LinearLayout>
下面我们将其引入到布局中
<?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" >
<include layout="@layout/top.xml" />
</RelativeLayout>
这样我们就达到了模块复用
(2)<viewstub>标签
viewstub标签和include标签是一样的,都是引用外部布局,不过相比之下它在解析布局文件时更加节省cpu
viewstub标签一般用来展示那些一般情况下不显示只有特殊情况才会显示的布局,例如网络错误,加载失败等等
下面以在一个布局main.xml中加入网络错误时的提示页面network_error.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" >
……
<ViewStub
android:id="@+id/network_error_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout="@layout/network_error" />
</RelativeLayout>
其中network_error.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/network_setting"
android:layout_width="@dimen/dp_160"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:text="@string/network_setting" />
<Button
android:id="@+id/network_refresh"
android:layout_width="@dimen/dp_160"
android:layout_height="wrap_content"
android:layout_below="@+id/network_setting"
android:layout_centerHorizontal="true"
android:layout_marginTop="@dimen/dp_10"
android:text="@string/network_refresh" />
</RelativeLayout>
在java中通过(ViewStub)findViewById(id)找到ViewStub,通过stub.inflate()展开ViewStub,然后得到子View
private View networkErrorView;
private void showNetError() {
// not repeated infalte
if (networkErrorView != null) {
networkErrorView.setVisibility(View.VISIBLE);
return;
}
ViewStub stub = (ViewStub)findViewById(R.id.network_error_layout);
networkErrorView = stub.inflate();
Button networkSetting = (Button)networkErrorView.findViewById(R.id.network_setting);
Button refresh = (Button)findViewById(R.id.network_refresh);
}
private void showNormal() {
if (networkErrorView != null) {
networkErrorView.setVisibility(View.GONE);
}
}
在上面showNetError()中展开了ViewStub,同时我们对networkErrorView进行了保存,这样下次不用继续inflate。这就是后面第三部分提到的减少不必要的infalte。
viewstub标签大部分属性同include标签类似。
使用merge标签,当引用的布局文件的根节点与所在布局的节点是同意节点,那么我们就使用merge标签,如果使用include标签,那么就会有多余的标签
二、去除不必要的嵌套和view节点
当我们在进行布局时,有些时候该view只有在特定的情况下才会展示出来,那么我们就可以将其gone掉
多使用RelativeLayout标签,在开始时我们的默认根节点是LinearLayout,在4.0以后就成了RelativeLayout,相比之下RelativeLayout的性能更加优越