在android布局中使用include和merge标签

在我们开发android布局时,经常会有很多的布局是相同的,这个时候我们可以通过<include/>和<merge/>标签实现将复杂的布局包含在需要的布局中,减少重复代码的编写。

 

1. 创建一个可以重复使用的布局:

如下代码描述在应用中每个acitivity都出现的顶栏titlebar.xml

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width=”match_parent”
    android:layout_height="wrap_content"
    android:background="@color/titlebar_bg">

    <ImageView android:id="@+id/title"
               android:layout_width="wrap_content"
               android:layout_height="wrap_content" 
               android:src="@drawable/gafricalogo" />
</FrameLayout>

上面的根布局(root view)即frameLayout会出现在之后插入的地方。

2. 使用<include/>标签:

在应用中的一个activity的布局中顶栏就是如上的布局,那么我们就可以include上面的titlebar.xml达到复用的效果,布局代码如下

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" 
    android:layout_width=”match_parent”
    android:layout_height=”match_parent”
    android:background="@color/app_bg"
    android:gravity="center_horizontal">

    <include  android:id="@+id/new_title"
              layout="@layout/titlebar"/>

    <TextView android:layout_width=”match_parent”
              android:layout_height="wrap_content"
              android:text="@string/hello"
              android:padding="10dp" />

    ...

</LinearLayout>

通过取得元素的id,我们可以修改include标签中元素的属性,下面的例子为在actiivty中修改titlebar中的图片:

private View mTitleBar = null;
private ImageView mTitleImageView = null;

mTitleBar = findViewById(R.id.new_title);
mTitleImageView = (ImageView)mTitleBar.findViewById(R.id.title);
mTitleImageView.setImageResource(R.drawable.logo);

在使用include标签时,我们可以覆写插入布局root view的属性(所有的android:layout_*属性)

<include android:id=”@+id/news_title”
         android:layout_width=”match_parent”
         android:layout_height=”match_parent”
         layout=”@layout/title”/>

如果需要覆写插入布局root view的属性,则必须制定android:layout_width和android:layout_height这两个属性以使其它的覆写属性生效。

 

3. 使用<merge/>标签

merge标签用来消除我们在include一个布局到另一个布局时所产生的冗余view group。比如现在很多布局中会有两个连续的Button,于是我们将这两个连续的Button做成可复用布局(re-usable layout)。在使用include标签时我们必须先将这两个Button用一个view group比如LinearLayout组织在一起然后供其它布局使用,如果是include的地方也是LiearLayout就会造成有两层连续的LiearLayout,除了降低UI性能没有任何好处。这个时候我们就可以使用<merge/>标签作为可复用布局的root view来避免这个问题。

<merge xmlns:android="http://schemas.android.com/apk/res/android">

    <Button
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content"
        android:text="@string/add"/>

    <Button
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content"
        android:text="@string/delete"/>

</merge>

当我们用<include/>标签复用上述代码时,系统会忽略merge元素,直接将两个连续的Button放在<include/>标签所在处。


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值