android--Merge和include标签的区别和使用

旧城等待, 2022-08-12 16:26 244阅读 0赞

原文摘自:http://www.cnblogs.com/travelfromandroid/articles/2133206.html

使用 标签来重用layout代码

如果在一个项目中需要用到相同的布局设计,可以通过 标签来重用layout代码,该标签在android开发文档中没有相关的介绍。这样可以多次引用一个布局片段而不用重复的复制、粘贴。通过include标签也可以覆写一些属性的值,例如上面的示例就覆写了引用的layout中的id值。

  1. Android layout文件中需要一个顶级容器来容纳其他的组件,而不能直接放置多个组件。
  2. 单独将<merge />标签做个介绍,是因为它在优化UI结构时起到很重要的作用。目的是通过删减多余或者额外的层级,从而优化整个Android Layout的结构。
标签闪亮登场了。当LayoutInflater遇到这个标签时,它会跳过它,并将内的元素添加到的父元素里。迷惑了吗?让我们用来替换FrameLayout,并重写之前的XML布局:







新的代码中,TextView和ImageView都直接添加到上一层的FrameLayout里。虽然视觉上看起来一样,但View的层次更加简单了:

3.png

很显然,在这个场合使用是因为Activity的ContentView的父元素始终是FrameLayout。如果你的布局使用LinearLayout作为它的根标签(举例),那么你就不能使用这个技巧。在其它的一些场合也很有用的。例如,它与标签结合起来就能表现得很完美。你还可以在创建一个自定义的组合View时使用。让我们看一个使用创建一个新View的例子——OkCancelBar,包含两个按钮,并可以设置按钮标签。下面的XML用于在一个图片上显示自定义的View:

  1. <merge
  2. xmlns:android="http://schemas.android.com/apk/res/android" xmlns:okCancelBar="http://schemas.android.com/apk/res/com.example.android.merge">
  3. <ImageView
  4. android:layout_width="fill_parent"
  5. android:layout_height="fill_parent"
  6. android:scaleType="center"
  7. android:src="@drawable/golden_gate" />
  8. <com.example.android.merge.OkCancelBar
  9. android:layout_width="fill_parent"
  10. android:layout_height="wrap_content"
  11. android:layout_gravity="bottom"
  12. android:paddingTop="8dip"
  13. android:gravity="center_horizontal"
  14. android:background="#AA000000"
  15. okCancelBar:okLabel="Save"
  16. okCancelBar:cancelLabel="Don't save" />
  17. </merge>

新的布局效果如下图所示:

4.png

OkCancelBar的代码很简单,因为这两个按钮在外部的XML文件中定义,通过LayoutInflate类导入。如下面的代码片段所示,R.layout.okcancelbar以OkCancelBar为父元素:

  1. public class OkCancelBar extends LinearLayout {
  2. public OkCancelBar(Context context, AttributeSet attrs) {
  3. super(context, attrs);
  4. setOrientation(HORIZONTAL);
  5. setGravity(Gravity.CENTER);
  6. setWeightSum(1.0f);
  7. LayoutInflater.from(context).inflate(R.layout.okcancelbar, this, true);
  8. TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.OkCancelBar, 0, 0);
  9. String text = array.getString(R.styleable.OkCancelBar_okLabel);
  10. if (text == null) text = "Ok";
  11. ((Button) findViewById(R.id.okcancelbar_ok)).setText(text);
  12. text = array.getString(R.styleable.OkCancelBar_cancelLabel);
  13. if (text == null) text = "Cancel";
  14. ((Button) findViewById(R.id.okcancelbar_cancel)).setText(text);
  15. array.recycle();
  16. }
  17. }

两个按钮的定义如下面的XML所示。正如你所看到的,我们使用标签直接添加两个按钮到OkCancelBar。每个按钮都是从外部相同的XML布局文件包含进来的,便于维护;我们只是简单地重写它们的id:

  1. <merge xmlns:android="http://schemas.android.com/apk/res/android">
  2. <include
  3. layout="@layout/okcancelbar_button"
  4. android:id="@+id/okcancelbar_ok" />
  5. <include
  6. layout="@layout/okcancelbar_button"
  7. android:id="@+id/okcancelbar_cancel" />
  8. </merge>

我们创建了一个灵活且易于维护的自定义View,它有着高效的View层次:

5.png

标签极其有用。然而它也有以下两个限制:

· 只能作为XML布局的根标签使用

· 当Inflate以开头的布局文件时,必须指定一个父ViewGroup,并且必须设定attachToRoot为true(参看inflate(int, android.view.ViewGroup, Boolean)方法)。

发表评论

表情:
评论列表 (有 0 条评论,244人围观)

还没有评论,来说两句吧...

相关阅读