`

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) 方法的说明 )。

分享到:
评论

相关推荐

    android include merge标签

    android include merge标签

    Android中include和merge标签的使用

    Android中include和merge标签的基本使用方法

    android中include和merge标记的区别和使用

    android中include和merge标记的区别和使用

    Android开发之merge结合include优化布局

    merge结合include优化android布局,效果不知道,个人感觉使用上也有很大的局限,不过还是了解一下,记录下来。 布局文件都要有根节点,但android中的布局嵌套过多会造成性能问题,于是在使用include嵌套的时候我们...

    Android中使用include标签和merge标签重复使用布局

    主要介绍了Android中使用include标签和merge标签重复使用布局,文中讲解了创建可复用布局的例子以及include标签和merge标签使用例子,需要的朋友可以参考下

    Android布局技巧之include、merge与ViewStub标签的巧用

    Android 官方提供了三个用来优化布局的标签,分别是include、merge与ViewStub,下面这篇文章主要给大家介绍了关于Android布局技巧之include、merge与ViewStub标签巧用的相关资料,需要的朋友可以参考下

    Android抽象布局——include、merge 、ViewStub详解

    主要介绍了 Android抽象布局——include、merge 、ViewStub详解,详细的介绍了三种布局各有的优势,有兴趣的同学可以参考一下。

    include标签的使用

    由于Android系统对硬件的要求较高,并且上层应用都是用Java(效率要比C++低)编写的,对程序的优化就成了...可以从以下几个地方下手:布局优化、数据库优化、使用异步加载数据、使用缓存技术、算法代码优化、使用线程池

    Android 腾讯微博客户端源码.zip

    4:布局方面的当然是最常用的ReleativeLayout,LinearLayout,FrameLayout,include,merge的应用 5:Menu菜单的使用 6:弹出菜单的使用,简单的弹出框,包含list的弹出框. 7:webview嵌入腾讯第三方授权页面的使用 8:最...

    Android 布局优化

    Android 布局优化,(含 include、Viewstub、merge的用法)

    Android布局技巧之合并布局

    我们已经有文章向你描述如何使用&lt;include&gt;标签来重用和共享你的布局代码。这篇文章将向你阐述&lt;merge&gt;标签的使用以及如何与&lt;include&gt;标签互补使用。 &lt;merge&gt;标签用于减少View树的层次来优化Android的布局。通过看一个...

    Bria Android Edition 3.0.4.1

    Bria Android Edition is a highly secure, standards-based mobile VoIP softphone that works over both 3G and Wi-Fi networks. Using the device’s existing contact list, Bria Android Edition facilitates ...

    Android腾讯微薄客户端源代码(非常劲爆)

    4:布局方面的当然是最常用的ReleativeLayout,LinearLayout,FrameLayout,include,merge的应用 5:Menu菜单的使用 6:弹出菜单的使用,简单的弹出框,包含list的弹出框. 7:webview嵌入腾讯第三方授权页面的使用 8:最常用...

    Android——Android lint工具项目资源清理详解

    Android——Android lint工具项目资源清理 最近维护的项目已经有两年多,经过很多前辈的迭代,项目并没有变得健壮...尽量使用include、merge、ViewStub标签,尽量不存在冗余嵌套及过于复杂布局,尽量使用GONE替换INVISIBL

    word源码java-Android-Interview-Gathered:Android-面试-集结

    Activity和Fragment的生命周期 加速Activity启动 精简onCreate中的代码 将耗时操作放到后台线程 优化布局文件( Hierarchy Viewer, Layoutopt) 缓存ListView Android多线程的几种方式 Handler.sendXXXMessage() ...

Global site tag (gtag.js) - Google Analytics