布局优化之include,merge,ViewStub的使用
一、<include/>
在以前Android开发中,由于ActionBar设计上的不统一以及兼容性问题,所以很多应用都自定义了一套自己的标题栏titlebar。标题栏我们知道在应用的每个界面几乎都会用到,在这里可以作为一个很好的示例来解释
下面是一个自定义的titlebar文件:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@color/titlebar_bg">
<ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/gafricalogo" /></FrameLayout>在应用中使用titlebar布局文件,我们通过
<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>在
布局示例如下:
<include android:id="@+id/news_title" android:layout_width="match_parent" android:layout_height="match_parent" layout="@layout/title"/>如果想使用
Layout parameter layout_height ignored unless layout_width is also specified on
tag
如果
二、<merge/>
下面通过一个简单的示例探讨一下
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <TextView android:id="@+id/textView" android:layout_width="match_parent" android:text="Hello World!" android:layout_height="match_parent" /></LinearLayout>一个线性布局中嵌套一个文本视图,主布局如下:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/layout_wrap" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" >
<include android:id="@+id/layout_import" android:layout_width="match_parent" android:layout_height="match_parent" layout="@layout/layout_text" />
</LinearLayout>通过hierarchyviewer我们可以看到主布局View树的部分层级结构如下图:
(http://www.sunnyang.com/wp-content/uploads/2016/04/layout_merge01.png)
现在讲嵌套布局跟布局标签更改为
<merge xmlns:android="http://schemas.android.com/apk/res/android" >
<TextView android:id="@+id/textView" android:layout_width="match_parent" android:layout_height="match_parent" android:text="Hello World!"/>
</merge>然后将主布局
(http://www.sunnyang.com/wp-content/uploads/2016/04/layout_merge02.png)
对比截图就可以发现上面的四层结构,现在已经是三层结构了。当我们使用
View android.view.LayoutInflater.inflate(int resource, ViewGroup root, boolean attachToRoot)
root不可少,attachToRoot必须为true。
三、ViewStub
在开发过程中,经常会遇到这样一种情况,有些布局很复杂但是却很少使用。例如条目详情、进度条标识或者未读消息等,这些情况如果在一开始初始化,虽然设置可见性View.GONE,但是在Inflate的时候View仍然会被Inflate,仍然会创建对象,由于这些布局又想到复杂,所以会很消耗系统资源。
ViewStub就是为了解决上面问题的,ViewStub是一个轻量级的View,它一个看不见的,不占布局位置,占用资源非常小的控件。
定义ViewStub布局文件
下面是一个ViewStub布局文件:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/layout_wrap" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" >
<ViewStub android:id="@+id/stub_image" android:layout_width="match_parent" android:layout_height="wrap_content" android:inflatedId="@+id/image_import" android:layout="@layout/layout_image" />
<ViewStub android:id="@+id/stub_text" android:layout_width="match_parent" android:layout_height="wrap_content" android:inflatedId="@+id/text_import" android:layout="@layout/layout_text" />
</LinearLayout>layout_image.xml文件如下(layout_text.xml类似):
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/layout_image">
<ImageView android:id="@+id/imageView" android:layout_width="match_parent" android:layout_height="wrap_content" />
</LinearLayout>加载ViewStub布局文件
动态加载ViewStub所包含的布局文件有两种方式,方式一使用使用inflate()方法,方式二就是使用setVisibility(View.VISIBLE)。
示例java代码如下:
private ViewStub viewStub;
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.layout_main2); viewStub = (ViewStub) findViewById(R.id.stub_image); //viewStub.inflate();//方式一 viewStub.setVisibility(View.VISIBLE);//方式二 ImageView imageView = (ImageView) findViewById(R.id.imageView); imageView.setImageResource(R.drawable.image);}示例View层级截图如下:
(http://www.sunnyang.com/wp-content/uploads/2016/04/viewstub_view.png)
ViewStub一旦visible/inflated,它自己就不在是View试图层级的一部分了。所以后面无法再使用ViewStub来控制布局,填充布局root布局如果有id,则会默认被android
由于ViewStub这种使用后即可就置空的策略,所以当需要在运行时不止一次的显示和隐藏某个布局,那么ViewStub是做不到的。这时就只能使用View的可见性来控制了。
layout_*相关属性与include标签相似,如果使用应该在ViewStub上面使用,否则使用在嵌套进来布局root上面无效。
ViewStub的另一个缺点就是目前还不支持merge标签。
四、小结
Android布局优化基本上就设计上面include、merge、ViewStub三个标签的使用。在平常开发中布局推荐使用RelativeLayout,它也可以有效减少布局层级嵌套。最后了将merge和include源码附上,ViewStub就是一个View,就不贴出来了。
Include源码
/** * Exercise <include /> tag in XML files. */public class Include extends Activity { @Override protected void onCreate(Bundle icicle) { super.onCreate(icicle); setContentView(R.layout.include_tag); }}Merge源码
/** * Exercise <merge /> tag in XML files. */public class Merge extends Activity { private LinearLayout mLayout;
@Override protected void onCreate(Bundle icicle) { super.onCreate(icicle);
mLayout = new LinearLayout(this); mLayout.setOrientation(LinearLayout.VERTICAL); LayoutInflater.from(this).inflate(R.layout.merge_tag, mLayout);
setContentView(mLayout); }
public ViewGroup getLayout() { return mLayout; }}