android性能优化——布局优化
2015-12-25 14:55
337 查看
一、使用抽象标签(1)<include>标签<include>标签,是将布局的公共部分抽取出来,以实现布局的模块化,例如我们一个app顶部的布局基本是固定,所以我们可以将其抽取出来以便共用
下面我们将其引入到布局中下面就是我们的公共布局top.xml<pre name="code" class="java"> <LinearLayout android:id="@+id/top" android:layout_width="match_parent" android:layout_height="54dp" android:background="#ffffff"> <RelativeLayout android:id="@+id/back" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1"> <ImageView android:id="@+id/img_back" android:layout_width="28dp" android:layout_height="28dp" android:background="@drawable/back" android:layout_centerVertical="true" android:layout_marginLeft="15dp"/> </RelativeLayout> <RelativeLayout android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="标题" android:textSize="19sp" android:textColor="#292d33" android:layout_centerInParent="true"/> </RelativeLayout> <RelativeLayout android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1"> </RelativeLayout> </LinearLayout>[code]
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" > <include layout="@layout/top.xml" /> </RelativeLayout>这样我们就达到了模块复用(2)<viewstub>标签viewstub标签和include标签是一样的,都是引用外部布局,不过相比之下它在解析布局文件时更加节省cpuviewstub标签一般用来展示那些一般情况下不显示只有特殊情况才会显示的布局,例如网络错误,加载失败等等下面以在一个布局main.xml中加入网络错误时的提示页面network_error.xml为例
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" > …… <ViewStub android:id="@+id/network_error_layout" android:layout_width="match_parent" android:layout_height="match_parent" android:layout="@layout/network_error" /> </RelativeLayout>其中network_error.xml为只有在网络错误时才需要显示的布局,默认不会被解析
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" > <Button android:id="@+id/network_setting" android:layout_width="@dimen/dp_160" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:text="@string/network_setting" /> <Button android:id="@+id/network_refresh" android:layout_width="@dimen/dp_160" android:layout_height="wrap_content" android:layout_below="@+id/network_setting" android:layout_centerHorizontal="true" android:layout_marginTop="@dimen/dp_10" android:text="@string/network_refresh" /> </RelativeLayout>在java中通过(ViewStub)findViewById(id)找到ViewStub,通过stub.inflate()展开ViewStub,然后得到子View
private View networkErrorView; private void showNetError() { // not repeated infalte if (networkErrorView != null) { networkErrorView.setVisibility(View.VISIBLE); return; } ViewStub stub = (ViewStub)findViewById(R.id.network_error_layout); networkErrorView = stub.inflate(); Button networkSetting = (Button)networkErrorView.findViewById(R.id.network_setting); Button refresh = (Button)findViewById(R.id.network_refresh);} private void showNormal() { if (networkErrorView != null) { networkErrorView.setVisibility(View.GONE); }}在上面showNetError()中展开了ViewStub,同时我们对networkErrorView进行了保存,这样下次不用继续inflate。这就是后面第三部分提到的减少不必要的infalte。viewstub标签大部分属性同include标签类似。(3)<merge>标签使用merge标签,当引用的布局文件的根节点与所在布局的节点是同意节点,那么我们就使用merge标签,如果使用include标签,那么就会有多余的标签二、去除不必要的嵌套和view节点当我们在进行布局时,有些时候该view只有在特定的情况下才会展示出来,那么我们就可以将其gone掉 多使用RelativeLayout标签,在开始时我们的默认根节点是LinearLayout,在4.0以后就成了RelativeLayout,相比之下RelativeLayout的性能更加优越
相关文章推荐
- Android-SlideSupport-ListLayouts 使用简介
- Android体系框架
- 圣诞节,写个程序练练手――――Android 全界面悬浮按钮实现
- android使用JNI进程守护service
- Android版添加phonegap--员工簿插件教程
- FragmFragment在安卓Activity里的使用
- Android Core框架使用手册
- Android 终止线程的方法
- 安卓小技巧整理【持续更新中……】
- 给 Android 开发者的 RxJava 详解
- Android 自定义PopupWindow技巧
- android自定义视图开发利器ViewDragHelper源码解析
- Android Topbar(自定义组合控件)
- 基于Android实现桌面悬浮清内存app概述
- android 编程实战 学习 之一
- Android 获取屏幕的信息
- android安全
- Android实际分辨率(android:anyDensity="false"的情况下的获取方法)
- android ListView 九大重要属性详细分析
- 关于android 怎样安装 assets文件下的apk