您的位置:首页 > 移动开发 > Android开发

Android里merge和include标签的使用

2012-05-02 16:33 411 查看
参考android文档:《Layout Tricks:Merging》

先得说下关于<merge />标签的第一个比较简单的用法。如果我们使用FrameLayout作为activity'scontent view的父元素(也就是在main.xml里把它写在最外层),那么可以考虑用<merge />替换<FrameLayout />标签。官方文档给出的解释是这样做可以减少一级布局层次达到优化布局的效果。这是文档里关于这部分结论的原文,个人E文水平有限,直接贴出来好了:

Obviously, using <merge /> worksin this case because the parent of an activity's content view is always aFrameLayout. You could not apply this trick if your layout was using aLinearLayout as its root tag for instance.

关于mergeinclude标签的使用,直接用实例说明吧。

TestMergeInclude.java

public class TestMergeInclude extends Activity {
/** Called when the activity is firstcreated. */
@Override
public void onCreate(BundlesavedInstanceState) {
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"/>

<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" />
</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
layout="@layout/okcancelbar_button"
android:id="@+id/okcancelbar_cancel" />
</merge>


okcancelbar_button.xml

<?xml version="1.0"encoding="utf-8"?>
<Button xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>


一点思考:

1.在OkCancelBar类的构造器中,我们看到一段稍微复杂点儿的代码。其实它们并不难,只是通过ID拿到某个特定的Button引用,然后为其设定要显示出来的文字。与直接使用android:text相比,这种设置要累赘得多。所以不妨把这个累赘看成是为利用<merge />的优点所付出的代价。这个优点就是组件重用,我们只在

okcancelbar_button.xml文件里声明了一个Button,但在使用时却拥有了两个(当然可以更多)Button。

2.这里可重用组件(这里的okcancelbar_button)的定义很关键,因为它关联着所有要使用它的功能点。比如:如果你确定在你的应用里,所有使用到这个button的地方需要的显示文字都一样,那你就可以在上面button的定义中再加一个android:text属性,这样就省去了使用时再去逐个设置的麻烦。另外,本例的okcancelbar_button里只定义了一个button,我想这里应该可以扩展到一个布局单元,比如LinearLayout,FrameLayout等等之类的。本人还没尝试,值得一做。

3.关于使用<merge />标签的一些限制:

(1)它只能作为XML布局声明的root元素来使用;

(2)使用它来inflate一个布局时,必须指定一个ViewGroup实例作为其父元素并且设置attachToRoot属性为true(
参考
inflate(int,
android
.view.ViewGroup, boolean)
方法的说明)。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: