日拱一卒(二十)
16lz
2022-06-11
Android布局优化:
(1)尽可能多的使用Relativelayout;---------------因为Android手机硬件很多,Relativelayout灵活能更好的适应与不同的android设备。
(2)尽可能使用include标签;-----------------include可以将相同的组件提取出来,以便其他布局服用,有利于后期维护。
(3)使用merge减少布局嵌套-------------------merge有两种使用方式:
①xml文件的根布局是FrameLayout。如:activity的布局main.xml根节点的父节点是FrameLayout,如果main.xml的根节点也说FrameLayout,则可以用merge代替Framelayout,减少一次Framelayout:
<merge xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:background="@android:color/darker_gray" android:layout_height="match_parent" > <include layout="@layout/common_navitationbar" /></merge>
②当用include标签导入一个共用布局时,如果父布局和子布局根节点为同一类型。
先看一个include的布局common_navi_right.xml:
(4) 使用< ViewStub />标签来加载一些不常用的布局 ------------------ ViewStub调用inflate()方法或设置visible之前,它是不占用布局空间和系统资源的。加载实例:请求网络超时的布局。
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="wrap_content" android:layout_height="wrap_content" > <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:text="Ok" android:textColor="@android:color/black" /></RelativeLayout>主 布局: common_navitationbar.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@android:color/white" android:padding="10dip" > <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:text="Back" android:textColor="@android:color/black" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:text="Title" android:textColor="@android:color/black" /> <include layout="@layout/common_center" /></RelativeLayout>
可以看到 common_navitationbar.xml中include的父节点和include中的根节点都是RelativieLayout,这是就可以将include中的RelativieLayout改写成merge,减少层次:
<merge xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="wrap_content" android:layout_height="wrap_content" > <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:text="Ok" android:textColor="@android:color/black" /></merge>
<merge xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:background="@android:color/darker_gray" android:layout_height="match_parent" > <include layout="@layout/common_navitationbar" /> <ViewStub android:id="@+id/msg_layout" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:layout="@layout/common_msg" /></merge>
如上例中msg_layout这个布局,在代码中如果没调用inflate()或者没设置visible,是不占用空间的。
原文详情地址
更多相关文章
- Android(安卓)中各种权限深入体验及详解
- Android学习记录使用Gallery实现炫丽的拖动效果
- 如何使用ndk中addr2line工具查询so库中错误信息行数
- Android零碎知识点(6)——下拉列表框Spinner的基本使用
- android中使用定时任务
- 关于Android中Toast使用小结
- 箭头函数的基础使用
- NPM 和webpack 的基础使用
- Python list sort方法的具体使用