您的位置:首页 > 其它

提升布局性能____Re-using Layouts with <include/>

2016-12-10 15:28 417 查看
可以再一个布局中通过“include”和“merge”元素进行复用其他的布局元素。
比如如下一个布局:

<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>


这是一个可复用的布局文件,倘若有以下的布局文件:

<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”/>


这样就重新调整了被包含者的属性。

 

android中海油另一个“merge”标签,看了官方文档后,我个人理解是这样:

假使主布局中的根元素是线性布局,通过include包含了一个子布局,该子布局的根元素仍然是线性布局,那么包含后就与主布局的线性元素重复了,所以可以通过“merge”来解决,

<merge
xmlns:android="http://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>


 

 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: