标题: Android的merge标签用法 [打印本页] 作者: 瀚海狼烟 时间: 2015-3-19 12:28 标题: Android的merge标签用法 先得说下关于<merge />标签的第一个比较简单的用法。如果我们使用FrameLayout作为activity's content view的父元素(也就是在main.xml里把它写在最外层),那么可以考虑用<merge />替换<FrameLayout />标签。官方文档给出的解释是这样做可以减少一级布局层次达到优化布局的效果。这是文档里关于这部分结论的原文,个人E文水平有限,直接贴出来好了:
Obviously, using <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);
}
}
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);