Android高效布局

如何实现高效的UI布局,在Android官方提供了三种布局方式  <include/>  <merge/> <ViewStub/>


一、首先来看<include/>  布局的重用性,顾名思义它能够重用布局文件。

简单的例子:main.xml

  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     android:orientation="vertical"   
  3.     android:layout_width=”match_parent”  
  4.     android:layout_height=”match_parent”  
  5.     android:background="@color/black"  
  6.     android:gravity="center_horizontal">  
  7.   
  8.     <include layout="@layout/title"/>       //重用
  9.   
  10.     <TextView android:layout_width=”match_parent”  
  11.               android:layout_height="wrap_content"  
  12.               android:text="@string/app"  
  13.               android:padding="10dp" />  
  14. </LinearLayout> 

              1.在设置了<include/> 标签的layout_width 和layout_height两个属性后,<include/>标签的android:layout_*都是可以使用的。

     2.<include/>标签可以使用单独的layout属性,这个也是必须使用的。

   


二、<merge/> 的使用减少布局层次结构  它有两种使用方式;

第一种使用方式:替换根布局FrameLayout

如果在一个布局文件中它的跟布局为<FrameLayout/>可以如下使用:


替换前:main.xml

<?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" >

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="@string/app" />

</FrameLayout>


替换后:main.xml

<?xml version="1.0" encoding="UTF-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android">


    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="@string/app_name" />

</merge>


第二种使用方式:当时用<include/>时,如果根布局和自布局的根节点相同,那么可以将子布局的跟节点使用<merge/>来代替


例如:

main.xml

<?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" >

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="@string/app" />

    <include layout="@layout/title"/>


</FrameLayout>



布局:title.xml   子布局根节点与父布局根节点一致是可以用<merge/>代替,减少布局层次结构

<?xml version="1.0" encoding="UTF-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="@string/app" />
</merge>



三、<ViewStub/> 的使用,它的最大优不会影响UI的初始化性能,例如各种不常用的进度条,提示信息都可使用<ViewStub/>来代替已减少你内存的使用量,加快渲染的速度。

<ViewStub/>是不可见的,大小为0的View,当你真正什么时候使用它时,它就会显示。

例如:main.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"
    android:orientation="vertical">
    
    <ViewStub
        android:id="@+id/viewstub"
        android:inflatedId="@+id/import"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout="@layout/message"></ViewStub>
</LinearLayout>



布局 message.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"
    android:orientation="vertical" >


    <TextView
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="center"
        android:text="正在加载..." />


</LinearLayout>



当你想加载布局时,可以使用下面其中一种方法:

  1. ((ViewStub) findViewById(R.id.import)).setVisibility(View.VISIBLE);  
  2. 或者
  3. View importPanel = ((ViewStub) findViewById(R.id.stub_import)).inflate();  


其他:实现高效的布局应注意以下几点:

1.LinearLayout中应慎用Layout_weight 在渲染UI时会绘制两次。

2.减少布局层次的嵌套,扁平化布局设计。

3.去除布局的跟布局(如:没有子元素子布局文件,或者没有背景,默认不会显示)和累赘的父控件。

4.使用<merge/> <include/> <ViewStub/>标签

5.背景的优化,使用Ninepatch更节省内存,透明化绘制。

6.为当前页面指定焦点控件<requsetFocus/>避免整个屏幕处于获取焦点状态。

7.Android 4.x最新布局标签GridLayout、以及space.



常用分析布局工具 :Lint能够帮助你检查布局文件中潜在的Bug和提供优化方案,Hierarchy Viewer Tool查看布局层次结构以及计算UI渲染时所耗费的时间:1.测量时,2.布局,3.绘画 每个所占用的时间。以后还会详细介绍这两个工具的详细使用情况,网上也有很多的教程。

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值