Android中布局优化能让咱们的界面绘制时间减小,用户感知到的卡顿时间也短,体验感天然而然也就上去了,Android中的布局优化能够从多个方面来入手,include,merge,ViewStub等等一些方面。php
咱们日常在画布局的时候常常碰到一些重复的共性布局,好比说标题栏,导航栏之类的,或者一些按钮之类的,咱们在使用的时候重复的写很是麻烦,增长了咱们工做量,使用include标签咱们就能够避免去写那些无聊的共性代码,其实这个include单独来说和咱们这一篇文章没什么关系,可是他结合merge使用就能够减小一层ViewGroup的包裹,这一个咱们在merge中去讲,下面贴出include的用法。java
<?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:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".MainActivity">
<include android:layout_width="match_parent" android:layout_height="50dp" layout="@layout/base_title_bar"/>
</LinearLayout>
复制代码
merge的存在乎义就是为告终合include来减小include引用过来的文件的外层ViewGroup,说白了就是用merge替代ViewGroup,虽然咱们使用merge结合include能够达到减小绘制一层ViewGroup的效果,merge的布局是取决于父布局的,父布局是什么,他就是什么,或者咱们能够这么理解,下面的代码咱们就至关于将base_title_bar.xml中的两个textview直接写入到LinearLayout中,可是这里有个问题,咱们的代码有时候include的xml与父布局所要的样式是不同的,就好比咱们当前引入的titlebar是要横向的,可是父布局是纵向的,这时候咱们又得去给include外层再加上一层ViewGroup,因此咱们在使用merge与include结合的时候要考虑清楚咱们共性布局之后可能会使用的场景。android
base_title_bar.xml工具
<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android">
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center" android:text="out" android:textSize="18sp" />
<TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" android:text="Title" android:textSize="18sp" />
</merge>
复制代码
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".MainActivity">
<include android:layout_width="match_parent" android:layout_height="50dp" layout="@layout/base_title_bar"/>
</LinearLayout>
复制代码
ViewStub咱们称之为懒加载,须要用到的时候就加载它,可是这里会有一个坑须要注意一下,就是ViewStub在显示以后就没法再隐藏了,由于ViewStub在显示的时候会将本身remove掉,替换成设置的view,这里咱们只要记住ViewStub的做用就是一次性的按需加载就好了。咱们使用到它的时候再按需加载,达到view在有须要的时候再渲染,以优化性能,下面是使用方法;布局
坑点:ViewStub中的inflate()只能调用一次,调用屡次会报错性能
<?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:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".MainActivity">
<ViewStub android:id="@+id/viewstub" android:layout_width="match_parent" android:layout_height="50dp" android:layout="@layout/base_title_bar" />
</LinearLayout>
复制代码
viewStub = (ViewStub) findViewById(R.id.viewstub);
//第一种,直接显示,不获取到view
if (viewStub != null) {
viewStub.setVisibility(View.VISIBLE);
}
//第二种,显示,而且获取到view
if (viewStub != null) {
View view = viewStub.inflate();//获取到的是咱们要显示出来的view
}
复制代码
一些其余的优化方式就是一些细节了,须要咱们平时画界面的时候本身多注意注意,而且养成习惯优化
1.子控件若是和父控件是一样的背景色时,子控件不须要再设定背景色了,用父控件的(能蹭一点是一点)。spa
2.尽可能减小没必要要的嵌套,咱们上面的include+merge的方式只是其中一种方式,咱们平时画界面的时候仍是得多注意一下能减小嵌套就减小嵌套(省着点用有限的资源)code
3.能用LinearLayout与FrameLayout的时候就不用RelativeLayout,毕竟RelativeLayout控件比LinearLayout与FrameLayout等复杂.xml
4.合理的借助工具来分析界面是否过分绘制,而且进行合理的避免