创建可重用的Layout

如果你已知道你需要重用的 Layout, 就先建一个新的 XML 文件并定Layout 。 

比如
<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> 标签, 可以在 Layout 中添加可重用的件。 比如, 里有一个Layout 需要包含
上面的那个
标题栏
<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>
你也可以覆写被添加的 Layout 的所有 Layout 参数(任何 android:layout_* 属性) , 通<include/> 中声明他来完成。
比如:
<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/> 标签在你嵌套 Layout 取消了 UI 层级中冗余的 ViewGroup 。 

比如, 如果你有一个 Layout 是一个直方向的LinearLayout, 其中包含连续View 可以在Layout 中重用, 那么你会做一个 LinearLayout 来包含这两View ,以便重用.不过,当使用一个 LinearLayout 为另一个 LinearLayout 的根种嵌套 LinearLayout 的方式除了慢你的 UI 性能外没有任何意了避免种情况, 你可以用 <merge> 元素来替代可重用 Layout 的根点。 例如:
<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>
在,当你要将Layout包含到一个Layout中时(并且使用了 <include/> 标签),会忽略 <merge> 标签,直接把Button 放到 Layout <include> 的所在位置

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值