Android优化:布局性能优化(一)

来源:互联网 发布:淘宝直播卖衣服要钱么 编辑:程序博客网 时间:2024/04/28 18:09
Android优化:布局性能优化(一)
1、include标签
1)、作用:实现复用
2)、使用:
a、布局文件:
action_bar_normal.xml <!--标题栏布局-->
activity_main.xml <!--主布局-->
b、需求:需要在主布局中包含设计好的标题栏布局,且不能在主布局中再次编写标题栏。
c、解决方案:
在activity_main.xml文件中,使用include标签引入action_bar_normal.xml文件即可。如下:
...
<include
android:id="@+id/action_bar"
layout="@layout/action_bar_normal"
android:layout_width="match_parent"
android:layout_height="48dp" />
...
d、注意点:
i、<include />标签可以使用单独的layout属性,这个也是必须使用的。
ii、)可以使用其他属性。<include />标签若指定了ID属性,而你的layout也定义了ID,则你的layout的ID会被覆盖,解决方案
iii、在include标签中所有的Android:layout_*都是有效的,前提是必须要写layout_width和layout_height两个属性。
vi、布局中可以包含两个相同的include标签,引用时可以给include添加id然后findviewbyid然后再继续findviewbyid或者bufferknife定位到局地控件

2、merge标签
1)、作用:减少嵌套层次
2)、使用:
a、需求:优化如下代码:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
...
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#aaaaaa"
android:text="这里是深灰" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#bbbbbb"
android:text="这里是略灰"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#cccccc"
android:text="这里是浅灰"/>
</LinearLayout>
...
</LinearLayout>
b、优化方案:
i、将绿色部分代码分离出来。other.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">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#aaaaaa"
android:text="这里是深灰" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#bbbbbb"
android:text="这里是略灰"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#cccccc"
android:text="这里是浅灰"/>
</merge>

布局文件预览效果图:

说明:merge标签中没有设置布局方式(水平or垂直)的属性,所以布局文件预览时候会使得控件重叠起来。
ii、在主布局中引用:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
...
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#aaaaaa"
android:text="这里是深灰" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#bbbbbb"
android:text="这里是略灰"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#cccccc"
android:text="这里是浅灰"/>
<include layout="@layout/other"/>
</LinearLayout>
...
</LinearLayout>
主页效果图:

说明:由于other.xml文件的merge标签没有布局方向属性,所以需要在include外层的父标签中设置布局方式android:orientation="vertical"

3)、注意点:
i、merge作为include的布局时,需要把merge标签作为该布局的根标签。
ii、当在使用include引用merge布局文件时,系统会默认忽略merge标签,而直接把merge子标签加载到引用的当前页面布局中
4)、使用场景:
i、多用于替换FrameLayout或者当一个布局包含另一个时,<merge/>标签消除视图层次结构中多余的视图组。
ii、例如:你的主布局文件是垂直布局,引入了一个垂直布局的include,而这时若include布局使用的LinearLayout就没意义了,使用的话反而减慢你的UI表现。这时可以使用<merge/>标签代替include布局中的LinearLayout,达到优化UI。

3、ViewStub标签
1)、定义:先占位为空白,当需要显示某个布局时可以自动让位给该布局
2)、作用:减少内存使用,加快渲染速度
3)、注意点:
a、需要时才会加载。不需要立刻显示的控件,如:进度条、显示错误消息等可使用ViewStub
b、、不会影响UI初始化时的性能。
c、是一个不可见的,大小为0的View。
d、ViewStub暂不支持 merge标签。
4)、使用:
a、标签格式(占位):
<ViewStub  
android:id="@+id/stub_import"  
android:inflatedId="@+id/panel_import"  
android:layout="@layout/progress_overlay"  
android:layout_width="match_parent"  
android:layout_height="wrap_content"  
android:layout_gravity="bottom" />  、
b、现实需要的布局:
i、方式一:
((ViewStub) findViewById(R.id.stub_import)).setVisibility(View.VISIBLE);  
ii、方式二:
View importPanel = ((ViewStub) findViewById(R.id.stub_import)).inflate();  
c、注意点:
当调用inflate()函数的时候,ViewStub被引用的资源替代,并且返回引用的view。 这样程序可以直接得到引用的view而不用再次调用函数findViewById()来查找了。

0 0
原创粉丝点击