android之merge布局

来源:互联网 发布:mac词典 编辑:程序博客网 时间:2024/06/14 01:53

android之merge布局

<merge />标签闪亮登场了。当LayoutInflater遇到这个标签时,它会跳过它,并将<merge />内的元素添加到<merge />的父元素里。迷惑了吗?让我们用<merge />来替换FrameLayout,并重写之前的XML布局:

<merge xmlns:android="http://schemas.android.com/apk/res/android">
<ImageView  
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:scaleType="center"
android:src="@drawable/golden_gate" />
<TextView
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:layout_marginBottom="20dip"
android:layout_gravity="center_horizontal|bottom"
android:padding="12dip"
android:background="#AA000000"
android:textColor="#ffffffff"
android:text="Golden Gate" />
</merge>

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

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

<merge
xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:okCancelBar="http://schemas.android.com/apk/res/com.example.android.merge">
<ImageView  
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
      android:scaleType="center"
android:src="@drawable/golden_gate" />
<com.example.android.merge.OkCancelBar
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
android:layout_gravity="bottom"
     android:paddingTop="8dip"
android:gravity="center_horizontal"
android:background="#AA000000"
 okCancelBar:okLabel="Save"
okCancelBar:cancelLabel="Don't save" />
</merge>

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

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

public class OkCancelBar extends LinearLayout {
public OkCancelBar(Context context, AttributeSet attrs) {
super(context, attrs);
setOrientation(HORIZONTAL);
   setGravity(Gravity.CENTER);
setWeightSum(1.0f);
LayoutInflater.from(context).inflate(R.layout.okcancelbar, this, true);
TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.OkCancelBar, 0, 0);
String text = array.getString(R.styleable.OkCancelBar_okLabel);
if (text == null) text = "Ok";
((Button) findViewById(R.id.okcancelbar_ok)).setText(text);
text = array.getString(R.styleable.OkCancelBar_cancelLabel);
if (text == null) text = "Cancel";
((Button) findViewById(R.id.okcancelbar_cancel)).setText(text);
array.recycle();
}
}

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

<merge xmlns:android="http://schemas.android.com/apk/res/android">
<include
layout="@layout/okcancelbar_button"
android:id="@+id/okcancelbar_ok" />        
<include
layout="@layout/okcancelbar_button"
   android:id="@+id/okcancelbar_cancel" />
</merge>

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

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

·         <merge />只能作为XML布局的根标签使用

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

绿色通道:好文要顶关注我收藏该文与我联系
启程去旅行
关注 - 1
粉丝 - 3
+加关注
1
0
    (请您对文章做出评价)   
»下一篇:1830

posted on 2011-08-10 08:03 启程去旅行 阅读(7520) 评论(2)  编辑 收藏

评论

#1楼2013-01-09 17:10wuxingxing

学习了。
支持(0)反对(0)
 

#2楼26668512013/4/26 10:25:042013-04-26 10:25Andy Zhai

学习了
支持(0)反对(0)
 

刷新评论刷新页面返回顶部
注册用户登录后才能发表评论,请 登录 或 注册,访问网站首页。
找优秀程序员,就在博客园
最新IT新闻:
·  美国国土安全部关闭Mt.Gox的移动支付帐号
·  超级计算机性能将会撞上墙?
·  Chakra Linux用独立的extra库取代BundleSystem
·  爱因斯坦行星:首次利用狭义相对论发现系外行星
·  英国无人驾驶客机成功实现测试飞行
» 更多新闻...
最新知识库文章:
·  指尖上的正则表达式–入门篇
·  软件开发中的11个系统思维定律
·  软件开发的人文关怀
·  将安全开发流程扩展到云和大数据
·  进程与线程的一个简单解释
» 更多知识库文章...
<2013年5月>日一二三四五六2829301234567891011121314151617181920212223242526272829303112345678

导航

  • 博客园
  • 首页
  • 新随笔
  • 联系
  • 订阅订阅
  • 管理

统计

  • 随笔 - 3
  • 文章 - 16
  • 评论 - 2
  • 引用 - 0

公告

昵称:启程去旅行
园龄:1年9个月
粉丝:3
关注:1
+加关注

搜索

常用链接

  • 我的随笔
  • 我的评论
  • 我的参与
  • 最新评论
  • 我的标签

随笔档案

  • 2012年1月 (1)
  • 2011年11月 (1)
  • 2011年8月 (1)

linux 嵌入

  • jni
  • sinamicro

最新评论

  • 1. Re:android之merge布局
  • 学习了
  • --Andy Zhai
  • 2. Re:android之merge布局
  • 学习了。
  • --wuxingxing

阅读排行榜

  • 1. 1830(73)
  • 2. I just came here to say hello-Martin Solveig(70)
  • 3. lighten my heart(18)

评论排行榜

  • 1. 1830(0)
  • 2. lighten my heart(0)
  • 3. I just came here to say hello-Martin Solveig(0)

推荐排行榜

原创粉丝点击