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  ,如需转载请自行联系原作者




相关文章
|
存储 消息中间件 人工智能
【05】AI辅助编程完整的安卓二次商业实战-消息页面媒体对象(Media Object)布局实战调整-按钮样式调整实践-优雅草伊凡
【05】AI辅助编程完整的安卓二次商业实战-消息页面媒体对象(Media Object)布局实战调整-按钮样式调整实践-优雅草伊凡
451 11
【05】AI辅助编程完整的安卓二次商业实战-消息页面媒体对象(Media Object)布局实战调整-按钮样式调整实践-优雅草伊凡
|
XML 存储 Java
【06】AI辅助编程完整的安卓二次商业实战-背景布局变更增加背景-二开发现页面跳转逻辑-替换剩余图标-优雅草卓伊凡
【06】AI辅助编程完整的安卓二次商业实战-背景布局变更增加背景-二开发现页面跳转逻辑-替换剩余图标-优雅草卓伊凡
353 3
【06】AI辅助编程完整的安卓二次商业实战-背景布局变更增加背景-二开发现页面跳转逻辑-替换剩余图标-优雅草卓伊凡
|
ARouter Android开发
Android不同module布局文件重名被覆盖
Android不同module布局文件重名被覆盖
|
编解码 Android开发
【Android Studio】使用UI工具绘制,ConstraintLayout 限制性布局,快速上手
本文介绍了Android Studio中使用ConstraintLayout布局的方法,通过创建布局文件、设置控件约束等步骤,快速上手UI设计,并提供了一个TV Launcher界面布局的绘制示例。
731 1
|
ARouter Android开发
Android不同module布局文件重名被覆盖
Android不同module布局文件重名被覆盖
1398 0
|
XML 前端开发 Android开发
android 前端常用布局文件升级总结(二)
android 前端常用布局文件升级总结(二)
249 0
|
XML 前端开发 Android开发
android 前端常用布局文件升级总结(一)
android 前端常用布局文件升级总结(一)
305 0
|
Android开发
【Android 安装包优化】Tint 着色器 ( 简介 | 布局文件中的 Tint 着色器基本用法 | 代码中使用 Tint 着色器添加颜色效果 )(二)
【Android 安装包优化】Tint 着色器 ( 简介 | 布局文件中的 Tint 着色器基本用法 | 代码中使用 Tint 着色器添加颜色效果 )(二)
550 0
【Android 安装包优化】Tint 着色器 ( 简介 | 布局文件中的 Tint 着色器基本用法 | 代码中使用 Tint 着色器添加颜色效果 )(二)
|
Android开发
【Android 安装包优化】Tint 着色器 ( 简介 | 布局文件中的 Tint 着色器基本用法 | 代码中使用 Tint 着色器添加颜色效果 )(一)
【Android 安装包优化】Tint 着色器 ( 简介 | 布局文件中的 Tint 着色器基本用法 | 代码中使用 Tint 着色器添加颜色效果 )(一)
932 0
【Android 安装包优化】Tint 着色器 ( 简介 | 布局文件中的 Tint 着色器基本用法 | 代码中使用 Tint 着色器添加颜色效果 )(一)
|
前端开发 Android开发 开发者
【Android 性能优化】布局渲染优化 ( 过渡绘制 | 自定义控件过渡绘制 | 布局文件层次深 | GPU 过渡绘制调试工具 | 背景过度绘制 )(一)
【Android 性能优化】布局渲染优化 ( 过渡绘制 | 自定义控件过渡绘制 | 布局文件层次深 | GPU 过渡绘制调试工具 | 背景过度绘制 )(一)
748 0
【Android 性能优化】布局渲染优化 ( 过渡绘制 | 自定义控件过渡绘制 | 布局文件层次深 | GPU 过渡绘制调试工具 | 背景过度绘制 )(一)

热门文章

最新文章