Android 布局巧用之include、merge、ViewStub
相信大家经常听到include
、merge
、ViewStub
这样的标签,官方也提到这三种布局可用于布局的优化。今天就介绍下这三种布局的使用,记录下来,便于后续app中的使用。
include
布局重用
app开发过程中,会遇到不同页面里有相同的布局,这时我们可以将这些通用的布局提取出来到一个单独的layout
文件里,再使用<include>
标签引入到相应的页面布局文件里,主要通过include
的layout
属性引用。
举个栗子include
的布局:
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/container" android:layout_width="match_parent" android:layout_height="wrap_content"> <TextView android:id="@+id/tv1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="这里是来自include布局" /> </RelativeLayout>
activity
的布局:
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:id="@+id/tv2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="以下的内容来自include标签" /> <include android:id="@+id/container" layout="@layout/include_layout" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/tv" android:layout_marginTop="10dp" /> </RelativeLayout>
这个标签在日常工作使用还是很常见的。这里有几点需要注意下:
1、如果给include
标签 和 include
所加载的布局 都添加id的话,那么id要保持一致,如例子中都是container
,否则是在代码中获取不到RelativeLayout
容器的。 当然我们可以避免这样的问题,只需要给其中一项添加id属性就可以。
2、include
布局里元素的id 要和 include
所在页面布局里的其他元素id 不同,如例子中的两个textview
,如果把id设置相同了,程序运行起来并不会报错,但是textview
的赋值只会赋值给其中的一个。
3、如果需要给include
标签设置位置属性的话,如例子中的layout_below
、layout_marginTop
,这时候 必须 同时设置include
标签的宽高属性layout_width
、layout_height
,否则编译器是会报错的。一般情况不需要设置include
的其他属性,直接加载布局文件 <include layout="@layout/...."/>
4、布局中可以包含两个相同的include标签,如下代码所示 两个include
都加载layout="@layout/include_layout"
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:id="@+id/tv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="以下的内容来自include标签" /> <include android:id="@+id/container" layout="@layout/include_layout" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/tv" android:layout_marginTop="10dp" /> <include android:id="@+id/container2" layout="@layout/include_layout" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="80dp" /> </RelativeLayout>
可以设置不同include
的id属性,引用的时候如下可以正常显示:
View view = findViewById(R.id.container2); TextView textView = view.findViewById(R.id.tv); textView.setText("这里是来自 第二个 include布局");
merge减少视图层级
merge
标签可用于减少视图层级来优化布局,可以配合include
使用,如果include
标签的父布局 和 include
布局的根容器是相同类型的,那么根容器的可以使用merge
代替。
页面布局
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <TextView android:id="@+id/tv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="以下的内容不是来自merge标签" /> <include layout="@layout/merge_layout" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="20dp" /> </LinearLayout>
先看没有使用merge
的:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content"> <TextView android:id="@+id/tv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="这里是不是来自merge布局" /> </LinearLayout>
看下view层的结构:
再看使用了merge
的:
<?xml version="1.0" encoding="utf-8"?> <merge xmlns:android="http://schemas.android.com/apk/res/android"> <TextView android:id="@+id/tv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="这里是来自merge布局" /> </merge>
view层结构:
可以看到对比,减少了一层的
LinearLayout
的嵌套,需要注意的是使用 merge
的布局,在 include
的标签设置距离属性没有生效,可以将一些间距属性设置到 include
布局里元素上,具体看项目需求使用。 ViewStub按需加载
按需加载 顾名思义需要的时候再去加载,不需要的时候可以不用加载,节约内存使用。通常情况我们会使用setVisibility
方法来控制视图的显示和隐藏,但是这种情况视图已经加载了。
比如app中页面里某个布局只需要在特定的情况下才显示,其余情况下可以不用加载显示,这时候可以使用ViewStub
。layout
属性是需要加载布局
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <ViewStub android:id="@+id/viewstub" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="20dp" android:layout="@layout/viewstub_layout" /> </LinearLayout>
需要注意的是 ViewStub
的inflate()
方法只能被调用一次,一旦调用后,ViewStub
将从视图中移除,被对应的layout
布局取代,同时会保留ViewStub
上设置的属性效果。
ViewStub viewstub = findViewById(R.id.viewstub); viewstub.inflate();
这篇关于include
、merge
、ViewStub
的使用就介绍到这里了,具体使用情况还得视项目而定。
最后附上github地址https://github.com/taixiang/include
欢迎关注我的博客:https://blog.manjiexiang.cn/
更多精彩欢迎关注微信号:春风十里不如认识你

低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。
持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。
转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。
- 上一篇
Android Camera2使用
1. 相机使用流程 图1.jpg 2. SurfaceView /** * Android 5.0 使用Camera2 照相 * 使用SurfaceView显示 * 主要步骤: * 1. 获得摄像头管理器CameraManager mCameraManager,mCameraManager.openCamera()来打开摄像头 * 2. 指定要打开的摄像头,并创建openCamera()所需要的CameraDevice.StateCallback stateCallback * 3. 在CameraDevice.StateCallback stateCallback中调用takePreview(),这个方法中,使用CaptureRequest.Builder创建预览需要的CameraRequest,并初始化了CameraCaptureSession,最后调用了setRepeatingRequest(previewRequest, null, childHandler)进行了预览 * 4. 点击拍照按钮,调用takePicture(),这个方法内,最终调用了capture(mCaptur...
- 下一篇
Android ViewPager和PagerAdapter简单代码写法
Android ViewPager和PagerAdapter简单代码写法 总是忘记,记下来备忘: package zhangphil.test; import android.os.Bundle; import android.support.annotation.NonNull; import android.support.annotation.Nullable; import android.support.v4.view.PagerAdapter; import android.support.v4.view.ViewPager; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.view.ViewGroup; import android.widget.ImageView; import java.util.ArrayList; public class ViewPagerActivity extends AppCompatActivity { p...
相关文章
文章评论
共有0条评论来说两句吧...
文章二维码
点击排行
推荐阅读
最新文章
- CentOS8安装Docker,最新的服务器搭配容器使用
- Linux系统CentOS6、CentOS7手动修改IP地址
- CentOS7安装Docker,走上虚拟化容器引擎之路
- Docker快速安装Oracle11G,搭建oracle11g学习环境
- Docker安装Oracle12C,快速搭建Oracle学习环境
- Docker使用Oracle官方镜像安装(12C,18C,19C)
- CentOS7,8上快速安装Gitea,搭建Git服务器
- MySQL8.0.19开启GTID主从同步CentOS8
- SpringBoot2配置默认Tomcat设置,开启更多高级功能
- CentOS8编译安装MySQL8.0.19