使用<include/> 对布局的重用
2013-05-17 21:02
295 查看
声明:有些地方翻译时候按照中文的逻辑进行了相应的调整。第一次翻译,难免有些错误,感谢指正。
原文链接:
Re-using Layouts with <include/>
当你需要创建一个可重用的复杂布局时,重用布局作用显得尤为重要。比如,一个是/否面板按钮,或一个带文字说明的自定义的进度条,被重用的元素意味着在可被内嵌到任何布局文件里面,并且被分开管理。因此,当你自定义一个视图来创建一个单独的UI控件时候,若有一个功能相同的布局,那你就可以直接重用它,效率更高一些。
如果你已经知道某个布局会被重用,那么就创建和定义它。比如,下面这个布局文件(titlebar.xml)就是引自G-Kenya codelab,它定义了一个标题栏,被多个activity所包含:
你可以将该设图添加到你想要放入的布局里面。
使用<include>标志
在你想要添加可重用的元素布局里面,添加<include/>标志。如,下面就是一个布局文件,里面包含了上面自定义的可重用titlebar.xml布局,也是引自G-Kenya codelab。
你也可以在<include/>标志里面覆盖根试图里面的布局属性(任何android:layout_* 结构的属性),如下:
但是,如果你想在使用<include>标志时候,覆盖布局其他属性,你必须同时覆盖android:layout_height 和 android:layout_width属性,这样其他属性被覆盖才能生效。
使用<merge>标志
使用<merge/>标志可以在不同布局内嵌层次结构中,消除多余的视图组,比如,如果你的主布局是一个verticalLinearLayout, 在这个布局里面,两个连续的视图可以被重用在其他布局里面,如果你将两个视图内嵌到其他试图里面,这两个视图需要它们自己的根试图,即上面那个vertical LinearLayout。但是当被内嵌到另一个vertical LinearLayout根试图时,就形成了一个vertical LinearLayout内嵌另一个vertical LinearLayout,被内嵌的那个vertical
LinearLayout不仅没有起到真正作用,反而降低了UI性能。
为了避免这些多余的试图层,你可以使用<merge>作为被重用布局的跟视图。如:
现在,当你使用<include/>标志时候,将这个布局内嵌到另一个布局里面,系统将会忽略<merge>元素,直接将这个两个button放入到被内嵌的布局里面,代替<include/>的位置。
BTW:这里面有一篇分析<merge/>使用的文章,非常不错,推荐给大家----->点击打开链接
原文链接:
Re-using Layouts with <include/>
使用<include/>重用布局
尽管Android提供了各式各样的控件以供人员在布局内重复使用,你也可能需要重用一些复杂的布局,那些布局都是一些自定义的布局。通过使用<include/><merge/>标志可将一个布局内嵌到另一个布局里面,从而提高重用布局效率。当你需要创建一个可重用的复杂布局时,重用布局作用显得尤为重要。比如,一个是/否面板按钮,或一个带文字说明的自定义的进度条,被重用的元素意味着在可被内嵌到任何布局文件里面,并且被分开管理。因此,当你自定义一个视图来创建一个单独的UI控件时候,若有一个功能相同的布局,那你就可以直接重用它,效率更高一些。
如果你已经知道某个布局会被重用,那么就创建和定义它。比如,下面这个布局文件(titlebar.xml)就是引自G-Kenya codelab,它定义了一个标题栏,被多个activity所包含:
<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>
你可以将该设图添加到你想要放入的布局里面。
使用<include>标志
在你想要添加可重用的元素布局里面,添加<include/>标志。如,下面就是一个布局文件,里面包含了上面自定义的可重用titlebar.xml布局,也是引自G-Kenya codelab。<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:layout_* 结构的属性),如下:
<include android:id=”@+id/news_title” android:layout_width=”match_parent” android:layout_height=”match_parent” layout=”@layout/title”/>
但是,如果你想在使用<include>标志时候,覆盖布局其他属性,你必须同时覆盖android:layout_height 和 android:layout_width属性,这样其他属性被覆盖才能生效。
使用<merge>标志
使用<merge/>标志可以在不同布局内嵌层次结构中,消除多余的视图组,比如,如果你的主布局是一个verticalLinearLayout, 在这个布局里面,两个连续的视图可以被重用在其他布局里面,如果你将两个视图内嵌到其他试图里面,这两个视图需要它们自己的根试图,即上面那个vertical LinearLayout。但是当被内嵌到另一个vertical LinearLayout根试图时,就形成了一个vertical LinearLayout内嵌另一个vertical LinearLayout,被内嵌的那个verticalLinearLayout不仅没有起到真正作用,反而降低了UI性能。
为了避免这些多余的试图层,你可以使用<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>
现在,当你使用<include/>标志时候,将这个布局内嵌到另一个布局里面,系统将会忽略<merge>元素,直接将这个两个button放入到被内嵌的布局里面,代替<include/>的位置。
BTW:这里面有一篇分析<merge/>使用的文章,非常不错,推荐给大家----->点击打开链接
相关文章推荐
- android_应用开发之(使用<include>标签重用布局)
- Android Developers:使用<include/>重用布局
- Android中View绘制优化二一---- 使用<include />标签复用布局文件
- <include>标签和BaseActivity联合使用简化代码结构
- JSP中使用哪个标签允许向<include/>标签传递参数
- JSP中使用哪个标签允许向<include/>标签传递参数
- Android UI 优化 使用<include/>和 <merge />标签
- JSP 使用<%@include%>报Duplicate local variable path 错误 解决方法
- Android UI 优化 使用<include/>和 <merge />标签
- 使用<jsp:include>,不想写死URL,动态生成URL的解决的方法
- JSP 中使用<%@include%> 报 Duplicate local variable path 错误的解决方法
- 【Android基础篇】使用<include>标签分割Layout
- HTML5 - 经典的上中下三段式布局(并使用<nav>实现侧边导航链接)
- Android UI 优化 使用<include/>和 <merge />标签
- 布局文件的引入<include>标签的使用
- jsp布局中关于<iframe>标签的使用
- <c:forEach>标签的使用
- <c:forEach>标签的使用
- <include layout="@*android:layout/preference_list_settings_header" />
- <jsp:include>和<%@include file=""%>区别