android布局优化

布局层级越多,过度绘制,浪费cpu就越多,手机加载速度就越慢,用户体验就越不好

1.尽量使用相对布局(Relativelayout

线性布局

[html]  view plain  copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:orientation="vertical">  
  6.   
  7.     <Button  
  8.         android:layout_width="wrap_content"  
  9.         android:layout_height="wrap_content"  
  10.         android:text="Button1"  
  11.         android:textSize="20sp"/>  
  12.   
  13.     <LinearLayout  
  14.         android:layout_width="match_parent"  
  15.         android:layout_height="match_parent"  
  16.         android:orientation="horizontal">  
  17.         <Button  
  18.             android:layout_width="wrap_content"  
  19.             android:layout_height="wrap_content"  
  20.             android:text="Button2"  
  21.             android:textSize="20sp"/>  
  22.         <Button  
  23.             android:layout_width="wrap_content"  
  24.             android:layout_height="wrap_content"  
  25.             android:text="Button3"  
  26.             android:textSize="20sp"/>  
  27.     </LinearLayout>  
  28. </LinearLayout>  

相对布局

[html]  view plain  copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent">  
  5.   
  6.     <Button  
  7.         android:id="@+id/button1"  
  8.         android:layout_width="wrap_content"  
  9.         android:layout_height="wrap_content"  
  10.         android:text="Button1"  
  11.         android:textSize="20sp"/>  
  12.     <Button  
  13.         android:id="@+id/button2"  
  14.         android:layout_width="wrap_content"  
  15.         android:layout_height="wrap_content"  
  16.         android:text="Button2"  
  17.         android:textSize="20sp"  
  18.         android:layout_below="@id/button1"/>  
  19.     <Button  
  20.         android:layout_width="wrap_content"  
  21.         android:layout_height="wrap_content"  
  22.         android:text="Button3"  
  23.         android:textSize="20sp"  
  24.         android:layout_toRightOf="@id/button2"  
  25.         android:layout_below="@id/button1"/>  
  26. </RelativeLayout>  

在这个简单的例子中可以看出线性布局比相对布局多出一层,理论来说在这个例子中相对布局比现行布局加载更快一点。

具体查看布局的层级可以用Android Studio自带的Hierarchy View.

2.善于使用抽象布局标签

    include:

 减少不必要、重复的布局

[html]  view plain  copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:orientation="vertical">  
  6.     <include layout="@layout/include_test1" />  
  7. </LinearLayout>  

include_test1.xml

[html]  view plain  copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent">  
  5.   
  6.     <Button  
  7.         android:layout_width="match_parent"  
  8.         android:layout_height="50dp"  
  9.         android:text="AAAAAAAAA"/>  
  10.   
  11. </LinearLayout>  


    merge删除多余的层级,可替换frameLayout,消除无用的布局。

    把include_test1.xml修改

[html]  view plain  copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <merge  xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent">  
  5.   
  6.     <Button  
  7.         android:layout_width="match_parent"  
  8.         android:layout_height="50dp"  
  9.         android:text="AAAAAAAAA"/>  
  10.   
  11. </merge>  

    ViewStub

    viewstub是一个轻量级、没有大小的view,并且不绘制任何事情,在需要的时候才加载它,不免不必要的资源浪费。

    在include_test.xml中添加viewstub

[html]  view plain  copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:orientation="vertical">  
  6.   
  7.     <include layout="@layout/include_test1" />  
  8.   
  9.     <ViewStub  
  10.         android:layout_width="match_parent"  
  11.         android:layout_height="wrap_content"  
  12.         android:id="@+id/ViewStub"  
  13.         android:layout="@layout/view_stub_test"/>  
  14. </LinearLayout>  

view_stub_test.xml

[html]  view plain  copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent">  
  5.   
  6.     <TextView  
  7.         android:layout_width="match_parent"  
  8.         android:layout_height="50dp"  
  9.         android:text="BBBBBBBBBB"  
  10.         android:gravity="center"/>  
  11. </LinearLayout>  

加载ViewStub布局使用setVisibility(View.VISIBLE);

[html]  view plain  copy
  1. setContentView(R.layout.include_test);  
  2.         btn = (Button) findViewById(R.id.btnLay);  
  3.         sv = (ViewStub) findViewById(R.id.ViewStub);  
  4.   
  5.   
  6.         btn.setOnClickListener(new View.OnClickListener() {  
  7.             @Override  
  8.             public void onClick(View v) {  
  9.                 sv.setVisibility(View.VISIBLE);  
  10.             }  
  11.         });  

3.android的约束布局ConstaintLayout

就是拖拽控件减少布局的层级

添加依赖

compile 'com.android.support.constraint:constraint-layout:1.0.0-alpha7'

新建约束布局

[html]  view plain  copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <android.support.constraint.ConstraintLayout  
  3.     xmlns:android="http://schemas.android.com/apk/res/android"  
  4.     xmlns:app="http://schemas.android.com/apk/res-auto"  
  5.     android:layout_width="match_parent"  
  6.     android:layout_height="match_parent">  
  7.   
  8.   
  9. </android.support.constraint.ConstraintLayout>  

点击编辑器下方的Design,就可以拖拽控件到布局上


添加约束条件


也可以在右侧手动填写约束条件的具体值

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值