android布局优化

本文介绍如何通过使用相对布局、抽象布局标签、约束布局等技巧减少Android应用的UI层级,从而提升加载速度和用户体验。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

布局层级越多,过度绘制,浪费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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值