Android布局优化----ViewStub、include、merge

1 StubView

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

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

使用示例:

(1)ViewStub中引用的布局

1
2
3
4
5
6
7
8
9
10
11
12
13
14
<? xml  version = "1.0"  encoding = "utf-8" ?>
< LinearLayout
     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" >
 
     < TextView
         android:id = "@+id/sbtv"
         android:layout_width = "300dp"
         android:layout_height = "300dp"
         android:text = "this is sub text view" />
 
</ LinearLayout >

(2)使用ViewStub

1
2
3
4
5
< ViewStub
     android:id = "@+id/viewstub"
     android:layout_width = "100dp"
     android:layout_height = "100dp"
     android:layout = "@layout/sublayout" />

(3)java代码中渲染加载

1
2
3
4
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)引用布局

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<? xml  version = "1.0"  encoding = "utf-8" ?>
< LinearLayout  xmlns:android = "http://schemas.android.com/apk/res/android"
     android:id = "@+id/sharedlayout2"
     android:layout_width = "200dp"
     android:layout_height = "200dp"
     android:background = "@android:color/black" >
 
     < TextView
         android:id = "@+id/mytv"
         android:layout_width = "100dp"
         android:layout_height = "100dp"
         android:background = "@android:color/holo_blue_light"
         android:text = "这时一个公用的布局" />
 
</ LinearLayout >

(2)使用include标签

1
2
3
4
5
<include
     android:id= "@+id/include1" <!--该id会覆盖布局( 1 )中LinearLayout的id;-->
     layout= "@layout/my"
     android:layout_width= "50dp"
     android:layout_height= "50dp" ></include>

(3)java中使用

1
2
3
LinearLayout sharedlayout = (LinearLayout) findViewById(R.id.include1);
TextView tv = (TextView) sharedlayout.findViewById(R.id.mytv);
tv.setText( "hello include1" );

示例2 引用merge布局

(1)引用布局

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
< merge  xmlns:android = "http://schemas.android.com/apk/res/android" >
 
     < LinearLayout
         android:id = "@+id/mergetlayout"
         android:layout_width = "wrap_content"
         android:layout_height = "wrap_content" >
 
         < TextView
             android:id = "@+id/mergetv1"
             android:layout_width = "40dp"
             android:layout_height = "40dp"
             android:background = "@android:color/holo_blue_bright"
             android:text = "merge text1"  />
 
         < TextView
             android:id = "@+id/mergetv2"
             android:layout_width = "40dp"
             android:layout_height = "40dp"
             android:background = "@android:color/darker_gray"
             android:text = "merge text2"  />
     </ LinearLayout >
</ merge >

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

1
2
3
< include
     android:id = "@+id/include2"
     layout = "@layout/mymerge" ></ include >

(3)java中使用

1
2
3
4
5
//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布局

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
< merge  xmlns:android = "http://schemas.android.com/apk/res/android" >
 
 
     < TextView
         android:id = "@+id/mergetv1"
         android:layout_width = "40dp"
         android:layout_height = "40dp"
         android:background = "@android:color/holo_blue_bright"
         android:text = "merge text1"  />
 
     < TextView
         android:id = "@+id/mergetv2"
         android:layout_width = "40dp"
         android:layout_height = "40dp"
         android:background = "@android:color/darker_gray"
         android:text = "merge text2"  />
</ merge >

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

(2)merge标签使用

1
2
3
< include
     android:id = "@+id/include2"
     layout = "@layout/mymerge" ></ include >

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

merge text 1
merge text 2
(4)java中使用
1
2
3
4
TextView mTV1 = (TextView)findViewById(R.id.mergetv1);
mTV1.setText( "hello merge tv1" );
TextView mtv2 = (TextView)findViewById(R.id.mergetv2);
mtv2.setText( "hello merge tv2" );

















本文转自wauoen51CTO博客,原文链接:http://blog.51cto.com/7183397/1847175  ,如需转载请自行联系原作者




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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值