Android里merge和include标签的使用【转】

参考android文档:《Layout Tricks:Merging》 先得说下关于<merge></merge>标签的第一个比较简单的用法。如果我们使用FrameLayout作为activity's content view的父元素(也就是在main.xml里把它写在最外层),那么可以考虑用<merge></merge>替换<framelayout></framelayout>标签。官方文档给出的解释是这样做可以减少一级布局层次达到优化布局的效果。这是文档里关于这部分结论的原文,个人E文水平有限,直接贴出来好了: Obviously, using <merge></merge> works in this case because the parent of an activity's content view is always a FrameLayout. You could not apply this trick if your layout was using a LinearLayout as its root tag for instance. 关于merge和include标签的使用,直接用实例说明吧。 TestMergeInclude.java public class TestMergeInclude extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); } } main.xml <?xml version="1.0" encoding="utf-8"?><merge xmlns:android="http://schemas.android.com/apk/res/android" xmlns:okcancelbar="http://schemas.android.com/apk/res/test.mergeinclude"><imageview android:layout_width="fill_parent" android:layout_height="fill_parent" android:scaletype="center" android:src="@drawable/wallpaper_rainbokeh"></imageview><test.mergeinclude.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"></test.mergeinclude.okcancelbar></merge> OkCancelBar.java public class OkCancelBar extends LinearLayout { public OkCancelBar(Context context) { super(context); // TODO Auto-generated constructor stub } public OkCancelBar(Context context, AttributeSet attrs) { super(context, attrs); // TODO Auto-generated constructor stub 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(); } } okcancelbar.xml <?xml version="1.0" encoding="utf-8"?><merge xmlns:android="http://schemas.android.com/apk/res/android"><include layout="@layout/okcancelbar_button" android:id="@+id/okcancelbar_ok"></include><include layout="@layout/okcancelbar_button" android:id="@+id/okcancelbar_cancel"></include></merge> okcancelbar_button.xml <?xml version="1.0" encoding="utf-8"?> 一点思考: 1.在OkCancelBar类的构造器中,我们看到一段稍微复杂点儿的代码。其实它们并不难,只是通过ID拿到某个特定的Button引用,然后为其设定要显示出来的文字。与直接使用android:text相比,这种设置要累赘得多。所以不妨把这个累赘看成是为利用<merge></merge>的优点所付出的代价。这个优点就是组件重用,我们只在 okcancelbar_button.xml文件里声明了一个Button,但在使用时却拥有了两个(当然可以更多)Button。 2.这里可重用组件(这里的okcancelbar_button)的定义很关键,因为它关联着所有要使用它的功能点。比如:如果你确定在你的应用里,所有使用到这个button的地方需要的显示文字都一样,那你就可以在上面button的定义中再加一个android:text属性,这样就省去了使用时再去逐个设置的麻烦。另外,本例的okcancelbar_button里只定义了一个button,我想这里应该可以扩展到一个布局单元,比如LinearLayout,FrameLayout等等之类的。本人还没尝试,值得一做。 3.关于使用<merge></merge>标签的一些限制: (1)它只能作为XML布局声明的root元素来使用; (2)使用它来inflate一个布局时,必须指定一个ViewGroup实例作为其父元素并且设置attachToRoot属性为true(参考 inflate(int, android.view.ViewGroup, boolean) 方法的说明 )。 http://hi.baidu.com/lck0502/blog/item/f22a1b13dccde8def6039ea4.html
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值