做网站前台有什么要求,泉州最专业微信网站建设公司,二级网站的建设方案,小程序商城和微商城的区别原文地址#xff1a;http://android.xsoftlab.net/training/improving-layouts/reusing-layouts.html
尽管Android提供了种类繁多的常用控件#xff0c;但是有时你可能希望重用一些比较复杂的布局。如果要重用这些布局#xff0c;可以使用 include/标签与 merg…原文地址http://android.xsoftlab.net/training/improving-layouts/reusing-layouts.html
尽管Android提供了种类繁多的常用控件但是有时你可能希望重用一些比较复杂的布局。如果要重用这些布局可以使用 include/标签与 merge/标签它们可将一个布局嵌入进另一个布局中。
可重用布局这项功能特别强大它可以使你创建那些复杂的可重用布局。比方说可以用来创建一个含有yes和no按钮的容器或者一个含有progressBar及一个文本框的容器。它还意味着程序可以对这些布局进行单独控制。所以虽然说你可以通过自定义View的方式来实现更为复杂的UI组件但是重用布局的方法更简便一些。
创建一个可重用的布局
如果你已经知道哪一个布局需要重用那么就创建一个新的xml文件用来定义这个布局。下面就定义了一个ActionBar的布局文件众所周知ActionBar是会在每个Activity中统一出现的
FrameLayout xmlns:androidhttp://schemas.android.com/apk/res/androidandroid:layout_width”match_parent”android:layout_heightwrap_contentandroid:backgroundcolor/titlebar_bgImageView android:layout_widthwrap_contentandroid:layout_heightwrap_content android:srcdrawable/gafricalogo /
/FrameLayout
使用 include/标签
在希望添加重用布局的布局内添加 include/标签。下面的例子就是将上面的布局加入到了当前的布局中
LinearLayout xmlns:androidhttp://schemas.android.com/apk/res/androidandroid:orientationvertical android:layout_width”match_parent”android:layout_height”match_parent”android:backgroundcolor/app_bgandroid:gravitycenter_horizontalinclude layoutlayout/titlebar/TextView android:layout_width”match_parent”android:layout_heightwrap_contentandroid:textstring/helloandroid:padding10dp /...
/LinearLayout
你也可以重写布局的参数但只仅限于以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标签可以帮助消除冗余的View容器。举个例子如果你的主布局是一个垂直的LinearLayout在它的内部含有两个View并且这两个View需要在多个布局中重用那么重用这两个View的布局需要有一个root View。然而使用单独的LinearLayout作为这个root View会导致在一个垂直的LinearLayout中又嵌了一个垂直的LinearLayout。其实这个内嵌的LinearLayout并不是我们真正想要的此外它还会降低UI性能。
为了避免出现这种冗杂的View容器你可以使用 merge标签作为这两个View的root View
merge xmlns:androidhttp://schemas.android.com/apk/res/androidButtonandroid:layout_widthfill_parent android:layout_heightwrap_contentandroid:textstring/add/Buttonandroid:layout_widthfill_parent android:layout_heightwrap_contentandroid:textstring/delete/
/merge
那么现在再使用这个布局的时候系统会自动忽略 merge标签并会将两个Button View直接加入到布局 include/标签所指定的位置。