目录:
文章目录
##1.使用< include>标签对**“重复代码”**进行复用
- < include>是我们开发过程中用到最多的一个标签
- 比如多个界面:都有同样的标题布局,这个标题.xml我们就可用include引入.
##2.使用< merge>标签**“减少视图层级”**的嵌套
- 1.新建一个项目和默认布局,打开hierarchyViewer,显示如下:
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-PqVBbJ6W-1658133234675)(https://img-blog.csdn.net/20160707085911857)]
(PS:最右边的RelativeLayout和TextView两层就是.xml中的布局内容) - 2.把< RelativeLayout>改成< merge>
<?xml version="1.0" encoding="utf-8"?>
<merge
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"/>
</merge>
运行的结果:
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-DhWSM82P-1658133234680)(https://img-blog.csdn.net/20160707090236214)]
(PS:可以看到,比之前少了一层RelativeLayout,却达到了相同的效果)
##3.可设置< ViewStub>标签进行**“延迟加载”**
-
1.介绍:
布局文件中的控件并不一定在程序启动时全都用到,有一些控件只在特定的情况下才会被使用到;因此急需一种机制来改变< include>标签的这种行为,只在需要时装载控件。这种机制就是是使用ViewStub控件.
-
2.具体原因介绍:
- < include>,有些控件并不一定在程序启动时全都用到,只在特定的情况下才会被使用到.
- 如:一个阅读图书的软件只有在下载电子书时才需要显示进度条,在平时看书时都是装载的本地电子书,并不需要使用进度条。因此,在程序启动时完全可以先不加载这个进度条。
- 但使用< include>标签引用这个包含进度条的布局文件时,会一股脑的把所有的控件全部装载到了内存中。大多数人认为,一个进度条不会占用多少资源,都装载也无妨。
- 这种想法没有错,但如果装载的不是进度条,而是很多ImageView控件(显示了很大的图像),并且不止在一个地方装载,那么手机资源可能会消耗殆尽。
- 因此,产生了一种机制来改进< include>标签的这种行为,只在需要时装载控件,所以要用到ViewStub控件。
- < include>,有些控件并不一定在程序启动时全都用到,只在特定的情况下才会被使用到.
-
3.设置了ViewStub.inflate后ViewStub才会装载引用.
代码演示:- 一个布局文件(方向上到下),包含1个按钮:
- 1.我的按钮
- 2.用< include>来引入一个layout
- 另一个布局lyout,包含上下方向的两个按钮
- 1.按钮1
- 2.按钮2
- 一个布局文件(方向上到下),包含1个按钮:
activity_main.xml
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="match_parent">
<Button
android:onClick="click"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="我的按钮"/>
<include layout="@layout/layout_button"/>
</LinearLayout>
layout_button.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="match_parent">
<Button
android:text="按钮1"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<Button
android:text="按钮2"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
正常运行得到的效果:
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-DJdn00Gc-1658133234682)(https://img-blog.csdn.net/20160707094223034)]
采用"延迟加载"机制:ViewStub,设定点击按钮1后再显示按钮1和2(为了模拟延迟,就把点击当做延迟).
我们把上面的< Include>变成ViewStub:
<!--<include layout="@layout/layout_button"/>-->
<ViewStub
android:id="@+id/vs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout="@layout/layout_button"/>
MainActivity.java
public void click(View view) {
ViewStub vs = (ViewStub) findViewById(R.id.vs);
vs.inflate();
}
得到的效果:
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-Gx1nbAZF-1658133234684)(https://img-blog.csdn.net/20160707101446659)]
点击后的效果:
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-EMTyKqNt-1658133234686)(https://img-blog.csdn.net/20160707101457472)]
获取ID
public void click(View view) {
ViewStub vs = (ViewStub) findViewById(R.id.vs);
View inflate = vs.inflate();
/*--------------可以获取填充布局的id,两种方法均可--------------*/
//这里的id想要获得就需要在xml里面+和填充xml一样的id.
int inflateId0 = vs.getInflatedId();
//这个是填充xml里的根id
int inflateId1 = inflate.getId();
Toast.makeText(this, "vs_id : " + inflateId0 + " inflateID : " + inflateId1, Toast.LENGTH_SHORT).show();
}
< ViewStub>可完全取代< include>,但< ViewStub>目前还无法取代< merge>