布局层级越多,过度绘制,浪费cpu就越多,手机加载速度就越慢,用户体验就越不好
1.尽量使用相对布局(Relativelayout)
线性布局
<?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">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button1"
android:textSize="20sp"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button2"
android:textSize="20sp"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button3"
android:textSize="20sp"/>
</LinearLayout>
</LinearLayout>
相对布局
<?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">
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button1"
android:textSize="20sp"/>
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button2"
android:textSize="20sp"
android:layout_below="@id/button1"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button3"
android:textSize="20sp"
android:layout_toRightOf="@id/button2"
android:layout_below="@id/button1"/>
</RelativeLayout>
在这个简单的例子中可以看出线性布局比相对布局多出一层,理论来说在这个例子中相对布局比现行布局加载更快一点。
具体查看布局的层级可以用Android Studio自带的Hierarchy View.
2.善于使用抽象布局标签
include:
减少不必要、重复的布局
<?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">
<include layout="@layout/include_test1" />
</LinearLayout>
include_test1.xml
<?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">
<Button
android:layout_width="match_parent"
android:layout_height="50dp"
android:text="AAAAAAAAA"/>
</LinearLayout>

merge删除多余的层级,可替换frameLayout,消除无用的布局。
把include_test1.xml修改
<?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">
<Button
android:layout_width="match_parent"
android:layout_height="50dp"
android:text="AAAAAAAAA"/>
</merge>
ViewStub
viewstub是一个轻量级、没有大小的view,并且不绘制任何事情,在需要的时候才加载它,不免不必要的资源浪费。
在include_test.xml中添加viewstub
<?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">
<include layout="@layout/include_test1" />
<ViewStub
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/ViewStub"
android:layout="@layout/view_stub_test"/>
</LinearLayout>
view_stub_test.xml
<?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">
<TextView
android:layout_width="match_parent"
android:layout_height="50dp"
android:text="BBBBBBBBBB"
android:gravity="center"/>
</LinearLayout>
加载ViewStub布局使用setVisibility(View.VISIBLE);
setContentView(R.layout.include_test);
btn = (Button) findViewById(R.id.btnLay);
sv = (ViewStub) findViewById(R.id.ViewStub);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
sv.setVisibility(View.VISIBLE);
}
});
3.android的约束布局ConstaintLayout
就是拖拽控件减少布局的层级
添加依赖
compile 'com.android.support.constraint:constraint-layout:1.0.0-alpha7'
新建约束布局
<?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">
</android.support.constraint.ConstraintLayout>
点击编辑器下方的Design,就可以拖拽控件到布局上
添加约束条件
也可以在右侧手动填写约束条件的具体值
版权声明:本文为博主原创文章,如需转载必须注明转载地址。 https://blog.youkuaiyun.com/yedekuqi4712/article/details/79411280