android merge跟include简单使用
转载:http://hi.baidu.com/lck0502/blog/item/f22a1b13dccde8def6039ea4.html
参考android文档:《Layout Tricks:Merging》
先得说下关于<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); } } 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"/> attrs.xml//有些文章没有加,编译不过 <?xml version="1.0" encoding="utf-8"?> <resources> <declare-styleable name="OkCancelBar"> <attr name="okLabel" format="string" /> <attr name="cancelLabel" format="string" /> </declare-styleable> </resources>
一点思考:
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)
方法的说明 )。