AndroidUI布局性能优化

1尽量多使用RelativeLayout来减少布局层级的嵌套
2多处相同的布局模块多用include进行引用;比如统一的标题栏(包括都有的主页返回按钮)
3使用标签来实现惰性加载,比如进度条再使用的时候才加载,不使用不加载
4使用可替代帧布局或相对布局减少布局层级的嵌套
标签示例代码

<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">
 
    <Button
        android:id="@+id/btn_show"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Show"/>
    <Button
        android:id="@+id/btn_gone"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="GONE"/>
 
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
    <ViewStub
        android:id="@+id/viewstub"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout="@layout/view"/>
    </LinearLayout>
 
</LinearLayout>

view布局

<?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">
    
    <ProgressBar
        android:id="@+id/progressBar1"
        style="?android:attr/progressBarStyleLarge"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
 
</LinearLayout>

标签示例代码
未使用merge标签的布局

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
             android:layout_width="match_parent"
             android:layout_height="match_parent">
    <ImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:src="@mipmap/ic_launcher"
        android:scaleType="fitXY"/>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="GreenRobot"
        android:layout_marginBottom="20dp"
        android:textColor="#000"
        android:background="#22000000"
        android:padding="12dp"
        android:layout_gravity="center_horizontal|bottom"/>
</FrameLayout>

使用merge标签的布局;减少了一个framlayout层级

<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android">
    <ImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:src="@mipmap/ic_launcher"
        android:scaleType="fitXY"/>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="GreenRobot"
        android:layout_marginBottom="20dp"
        android:textColor="#000"
        android:background="#22000000"
        android:padding="12dp"
        android:layout_gravity="center_horizontal|bottom"/>
</merge>

深入了解merge
https://www.jianshu.com/p/fbcc6a17d11e

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值