Android-减少页面层级,减少布局嵌套的实践 ConstraintLayout的好处及使用 Android中RelativeLayout和LinearLayout性能分析

上次转载了两篇文章,都是布局优化的文章.

ConstraintLayout的好处及使用

https://blog.csdn.net/yang1349day/article/details/80410224


Android中RelativeLayout和LinearLayout性能分析

https://blog.csdn.net/yang1349day/article/details/80359064


后来无论是在群里还是朋友发来的求助,碰到的一个问题就是点击事件和滑动冲突.我让他们把布局给我看看.结果大吃一惊,居然嵌套了5层以上,我的天,这解决起来是很棘手的哦!


此处给出一个例子,看我是如何做到一层布局,完成一个界面的.


以上这个布局我只用了一层去绘制,仅仅一层,恩是的! 如果是你的话,可以猜猜自己需要多少层呢?


此处给出我的xml布局文件:

<?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"
                                             xmlns:tools="http://schemas.android.com/tools"
                                             android:layout_width="match_parent"
                                             android:layout_height="match_parent"
                                             android:background="@color/white"
                                             android:orientation="vertical"
    >

    <TextView
        android:id="@+id/seller_recharge_title_txt"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="@dimen/px30"
        android:layout_marginStart="16dp"
        android:layout_marginTop="@dimen/px40"
        android:text="请选择充值金额"
        android:textColor="@color/didi_def_txt"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"/>


    <TextView
        android:id="@+id/seller_recharge_min_amount_txt"
        android:layout_width="@dimen/px320"
        android:layout_height="@dimen/px100"
        android:layout_below="@+id/seller_recharge_title_txt"
        android:layout_marginLeft="@dimen/px30"
        android:layout_marginStart="@dimen/px30"
        android:layout_marginTop="@dimen/px40"
        android:background="@drawable/seller_charge_recharge_bg_shape_corners_8_shape"
        android:gravity="center"
        android:text="¥ 100"
        android:textColor="@color/seller_charge_recharge_txt_orange"
        android:textSize="@dimen/font_28px"
        app:layout_constraintEnd_toStartOf="@+id/seller_recharge_input_amount_edit"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintHorizontal_chainStyle="spread_inside"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/seller_recharge_title_txt"/>

    <EditText
        android:id="@+id/seller_recharge_input_amount_edit"
        android:layout_width="@dimen/px320"
        android:layout_height="@dimen/px100"
        android:layout_alignParentRight="true"
        android:layout_below="@+id/seller_recharge_title_txt"
        android:layout_marginEnd="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="20dp"
        android:background="@drawable/seller_charge_recharge_bg_shape_corners_8_shape"
        android:gravity="center"
        android:hint="其他金额"
        android:textColor="@color/seller_charge_recharge_txt_orange"
        android:textColorHint="@color/seller_charge_recharge_txt_orange"
        android:textCursorDrawable="@drawable/shape_seller_input_info_edittext_cursor_color"
        android:textSize="@dimen/font_26px"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toEndOf="@+id/seller_recharge_min_amount_txt"
        app:layout_constraintTop_toBottomOf="@+id/seller_recharge_title_txt"/>

    <TextView
        android:id="@+id/seller_recharge_lower_money_title_txt"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/seller_recharge_input_amount_edit"
        android:layout_marginEnd="60dp"
        android:layout_marginRight="60dp"
        android:layout_marginTop="@dimen/px14"
        android:text="最低100元起"
        android:textColor="@color/didi_customer_evaluate_time_txt_gray"
        android:textSize="@dimen/font_20px"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/seller_recharge_input_amount_edit"/>

    <TextView
        android:id="@+id/seller_recharge_type_txt"
        android:layout_width="match_parent"
        android:layout_height="@dimen/px90"
        android:layout_below="@+id/seller_recharge_lower_money_title_txt"
        android:layout_marginTop="@dimen/px70"
        android:background="@color/seller_recharge_bg_gray"
        android:paddingLeft="@dimen/px30"
        android:paddingTop="@dimen/px40"
        android:text="请选择支付方式"
        android:textColor="@color/didi_customer_evaluate_time_txt_gray"
        android:textSize="@dimen/font_24px"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/seller_recharge_min_amount_txt"/>

    <RadioGroup
        android:id="@+id/seller_recharge_type_rg"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/seller_recharge_type_txt"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/seller_recharge_type_txt">

        <RadioButton
            android:id="@+id/seller_recharge_weixin_rb"
            android:layout_width="match_parent"
            android:layout_height="@dimen/px100"
            android:button="@null"
            android:drawableLeft="@mipmap/image_wx_play_icon"
            android:drawablePadding="@dimen/px20"
            android:drawableRight="@drawable/selector_item_def_checked"
            android:paddingLeft="@dimen/px20"
            android:paddingRight="@dimen/px20"
            android:text="微信"/>

        <View
            android:id="@+id/divider_line_1"
            android:layout_width="match_parent"
            android:layout_height="@dimen/px1"
            android:layout_below="@+id/seller_recharge_pay_by_wx_rl"
            android:background="@color/yeyou_def_bg_gray"/>

        <RadioButton
            android:id="@+id/seller_recharge_weixin_zfb"
            android:layout_width="match_parent"
            android:layout_height="@dimen/px100"
            android:button="@null"
            android:drawableLeft="@mipmap/image_zfb_play_icon"
            android:drawablePadding="@dimen/px20"
            android:drawableRight="@drawable/selector_item_def_checked"
            android:paddingLeft="@dimen/px20"
            android:paddingRight="@dimen/px20"
            android:text="支付宝"/>
    </RadioGroup>

    <View
        android:id="@+id/seller_recharge_bg"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_below="@+id/seller_recharge_type_rg"
        android:background="@color/yeyou_def_bg_gray"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/seller_recharge_type_rg"
        app:layout_constraintVertical_bias="0.0"/>

    <Button
        android:id="@+id/seller_recharge_pay_bt"
        android:layout_width="match_parent"
        android:layout_height="@dimen/px98"
        android:layout_below="@+id/seller_recharge_type_rg"
        android:layout_marginLeft="@dimen/px30"
        android:layout_marginRight="@dimen/px30"
        android:layout_marginTop="36dp"
        android:background="@drawable/shape_certificer_good1_bg"
        android:text="确认支付"
        android:textColor="@color/white"
        android:textSize="@dimen/font_32px"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="@+id/seller_recharge_bg"/>

</android.support.constraint.ConstraintLayout>

此处是详细图:



没错,只用了一层. 而且用到了constraintLayout这个控件去绘制. 之前是用的relativeLayout去绘制的一层,但是也遇到了很多适配的问题. 后来用了constraintLayout之后,所有问题几乎全部解决!!

包括:直接拖动控件和布局,设置百分比适配,不用自己一点一点的去绘制了,极大的提升了效率! 当然这里还有很多好玩的属性,具体可以看这篇文章:

https://blog.csdn.net/yang1349day/article/details/80410224


当然,上面的界面也不是很复杂,但是如果你想办法去减少嵌套的话任何复杂的界面都可以只有一层,相信自己. 我自己做的app都是几乎嵌套很少的

对于复杂列表页面,一般是用recyclerView完成的,对于recyclerView减少嵌套布局的话,开源库vLayout的效率是很高的,此处是地址

https://github.com/alibaba/vlayout 大家也可以学习一下.


优化布局的方法,有很多比如viewStub,merge,include标签,自定义style减少重复代码等等,需要大家自己去学习和理解.

此处是官网:https://developer.android.google.cn/training/improving-layouts/


以上讲的很全面,曾经想翻译然后写出来,但是翻译的文章太多了,就不去造轮子了.

使用google浏览器是自带翻译功能的.而且掘金上面是有翻译的文章的


每一次去写代码都要去想想有没有更好的办法,推翻以前的自己,高效的去工作,思考!



猜你喜欢

转载自blog.csdn.net/yang1349day/article/details/80540910