一 什么是布局优化
布局优化就是减少视图嵌套层级,减少视图层级可以有效的减少内存消耗,因为视图是一个树形结构,每次刷新和渲染都会遍历一次。
此标签只会加载一次,加载后会把所有子控件交付给其父控件,自身会移除。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.optimize.project.MainActivity">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="inflate" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="setdata"/>
<ViewStub
android:id="@+id/vs"
android:layout="@layout/view_stub"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<!--<include layout="@layout/merge_layout"/>-->
</LinearLayout>
view_stub.xml代码:
<?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"
android:layout_gravity="center"
android:orientation="vertical">
<TextView
android:id="@+id/hello_tv"
android:layout_centerInParent="true"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="DATA EMPTY!"/>
</RelativeLayout>
Activity代码:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
viewStub = (ViewStub) findViewById(R.id.vs);
/**空指针 由于viewStub 是延迟加载 必须调用 inflate 才会加载 */
//textView = (TextView) findViewById(R.id.hello_tv);
viewStub.inflate();
/** 空指针 由于viewStub加载后,将会把其子控件交给其父控件 自己将会隐退 所以此时viewStub 为null */
//textView = (TextView) viewStub.findViewById(R.id.hello_tv);
/** 正确 查找 */
textView = (TextView) findViewById(R.id.hello_tv);
textView.setText("查找了");
}
更直观的效果如图:
三 include标签
<include
layout="@layout/activity_main" />
可以通过这个标签直接加载外部的xml到当前结构中,是复用UI资源的常用标签。
四 merge标签
首先要知道merge不是控件,不具有View的一切属性,但它可以作为View的根标签使用,具有合并的意思。
如下下面的代码自定义组件:
<?xml version="1.0" encoding="utf-8"?>
<!-- 习惯性的标记一下,MergeLayout布局 android:orientation="vertical" -->
<merge xmlns:android="http://schemas.android.com/apk/res/android" >
<TextView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1.0"
android:background="#000000"
android:gravity="center"
android:text="第一个TextView"
android:textColor="#ffffff" />
<TextView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1.0"
android:background="#ffffff"
android:gravity="center"
android:text="第一个TextView"
android:textColor="#000000" />
</merge>
MergeLayout代码:
public class MergeLayout extends LinearLayout {
public MergeLayout(Context context) {
super(context);
// 设置为数值方向的布局
setOrientation(VERTICAL);
LayoutInflater.from(context).inflate(R.layout.merge_llayout, this, true);
}
}
直接使用图说明
使用LinerLayout作为根标签:
使用merge作为根标签:
哪些情况可以使用merge标签:
- merge必须放在布局文件的根节点上。
- merge并不是一个ViewGroup,也不是一个View,它相当于声明了一些视图,等待被添加。
- merge标签被添加到A容器下,那么merge下的所有视图将被添加到A容器下。
- 因为merge标签并不是View,所以在通过LayoutInflate.inflate方法渲染的时候, 第二个参数必须指定一个父容器,且第三个参数必须为true,也就是必须为merge下的视图指定一个父亲节点。
- 如果Activity的布局文件根节点是FrameLayout,可以替换为merge标签,这样,执行setContentView之后,会减少一层FrameLayout节点。
- 自定义View如果继承LinearLayout,建议让自定义View的布局文件根节点设置成merge,这样能少一层结点。
- 因为merge不是View,所以对merge标签设置的所有属性都是无效的。
五 多使用相对布局/约束布局(ConstraintLayout)
如下下面的布局:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/txt"
android:text="姓名:"
android:gravity="center"
android:paddingLeft="10dp"
app:layout_constraintRight_toLeftOf="@+id/edt"
android:layout_width="wrap_content"
android:layout_height="45dp" />
<EditText
android:id="@+id/edt"
android:hint="请输入姓名"
android:paddingRight="10dp"
app:layout_constraintLeft_toRightOf="@+id/txt"
android:layout_width="wrap_content"
android:layout_height="45dp" />
</android.support.constraint.ConstraintLayout>
说明,这个布局使用了约束布局,在水平轴上子控件之间双向指向形成一个链条,我们给txt设置左内边距,给edt设置右内边距,这两个属性将会作用于整个链条。如果我们使用LinerLayout作为根标签,实现相同的效果,需要这样如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:text="姓名:"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>
说明,很明显多了一层嵌套,我们知道加载布局的时候按照树状结构,多一层嵌套内存将会多一层压力。