android布局merge,Android布局优化----ViewStub、include、merge(示例代码)

1 StubView

作用:StubView标签中的布局只有在需要的时候才会被渲染加载。

注意:StubView的渲染加载操作只能执行一次;不支持merge标签

使用示例:

(1)ViewStub中引用的布局<?xml  version="1.0" encoding="utf-8"?>

android:id="@+id/stublayout"

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

android:orientation="vertical" android:layout_width="match_parent"

android:layout_height="match_parent">

android:id="@+id/sbtv"

android:layout_width="300dp"

android:layout_height="300dp"

android:text="this is sub text view"/>

(2)使用ViewStub

android:id="@+id/viewstub"

android:layout_width="100dp"

android:layout_height="100dp"

android:layout="@layout/sublayout"/>

(3)java代码中渲染加载ViewStub stub = (ViewStub)findViewById(R.id.viewstub);

stub.inflate();

TextView stubtv = (TextView)layout.findViewById(R.id.sbtv);

stubtv.setText("hello stub!");

2 include标签

作用:将引用的布局替换到当前布局中该标签所处的位置;

注意:引用的布局非merge,设置inlude的id属性后会覆盖掉引用布局顶层layout的id;

示例1 引用布局非merge

(1)引用布局<?xml  version="1.0" encoding="utf-8"?>

android:id="@+id/sharedlayout2"

android:layout_width="200dp"

android:layout_height="200dp"

android:background="@android:color/black">

android:id="@+id/mytv"

android:layout_width="100dp"

android:layout_height="100dp"

android:background="@android:color/holo_blue_light"

android:text="这时一个公用的布局"/>

(2)使用include标签

android:id="@+id/include1"

layout="@layout/my"

android:layout_width="50dp"

android:layout_height="50dp">

(3)java中使用LinearLayout sharedlayout = (LinearLayout) findViewById(R.id.include1);

TextView tv = (TextView) sharedlayout.findViewById(R.id.mytv);

tv.setText("hello include1");

示例2 引用merge布局

(1)引用布局

android:id="@+id/mergetlayout"

android:layout_width="wrap_content"

android:layout_height="wrap_content">

android:id="@+id/mergetv1"

android:layout_width="40dp"

android:layout_height="40dp"

android:background="@android:color/holo_blue_bright"

android:text="merge text1" />

android:id="@+id/mergetv2"

android:layout_width="40dp"

android:layout_height="40dp"

android:background="@android:color/darker_gray"

android:text="merge text2" />

(2)include标签:merge布局没有id属性,所以这里的id其实没有意义

android:id="@+id/include2"

layout="@layout/mymerge">

(3)java中使用//LinearLayout layout = (LinearLayout)findViewById(R.id.include2);//通过该代码无法获取(1)                                                    中的LinearLayout,这里的layout为null

TextView mTV1 = (TextView)findViewById(R.id.mergetv1);

mTV1.setText("hello merge tv1");

TextView mtv2 = (TextView)findViewById(R.id.mergetv2);

mtv2.setText("hello merge tv2");

3 merge标签

merge标签相当于控件的简单集合,被include到其他布局中后根据所处容器布局结合各个控件的相关属性进行布局。

注意:merge标签没有id属性

使用示例:

(1)merge布局

android:id="@+id/mergetv1"

android:layout_width="40dp"

android:layout_height="40dp"

android:background="@android:color/holo_blue_bright"

android:text="merge text1" />

android:id="@+id/mergetv2"

android:layout_width="40dp"

android:layout_height="40dp"

android:background="@android:color/darker_gray"

android:text="merge text2" />

此时的布局为两个textview重叠并且mergetv2处于上层;

(2)merge标签使用

android:id="@+id/include2"

layout="@layout/mymerge">

(3)由于该include标签处于一个竖直的linearlayout中,此时merge布局的显示效果如下:

merge text 1

merge text 2(4)java中使用TextView mTV1 = (TextView)findViewById(R.id.mergetv1);

mTV1.setText("hello merge tv1");

TextView mtv2 = (TextView)findViewById(R.id.mergetv2);

mtv2.setText("hello merge tv2");

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值