提高显示布局文件的性能 2 - 使用include标签重用Layout
Re-using Layouts with <include/>
尽管Android提供了很多种小的组件可以重用,我们还需要自定义一些稍微复杂一点的小组件进行重用。我们可以使用<include/> and <merge/> 标签来对当前的layout嵌入一些其他的layout.
在创建一个稍微复杂一点的layout时,重用layout是个很给力的方法。比如我们需要一个YES/NO的控制栏,包含文字提示的Progress bar。这意味着我们可以在很多地方重用那些自定义的layout.
例如:下面定义了一个需要在每个Activity都需要显示的titlebar.xml
- <FrameLayout xmlns:android="https://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>
Use the <include> Tag [使用<include>标签]
下面示例了一个包含了titlebar控件的布局:- <LinearLayout xmlns:android="https://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里面的属性,例如:
- <include android:id=”@+id/news_title”
- android:layout_width=”match_parent”
- android:layout_height=”match_parent”
- layout=”@layout/title”/>
Use the <merge> Tag [使用<merge>标签]
某些时候,自定义可重用的布局包含了过多的层级标签,比如我们需要在LinearLayout里面嵌入一个重用的组件,而恰恰这个自定义的可重用的组件根节点也是LinearLayout,这样就多了一层没有用的嵌套,无疑这样只会拖慢程序速度。而这个时候如果我们使用merge根标签就可以避免那样的问题。例如:
- <merge xmlns:android="https://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>
最后更新:2017-04-04 07:03:06