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:

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

(4) 使用< ViewStub />标签来加载一些不常用的布局 ------------------ ViewStub调用inflate()方法或设置visible之前,它是不占用布局空间和系统资源的。加载实例:请求网络超时的布局。

<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,是不占用空间的。


原文详情地址

更多相关文章

  1. Android自学笔记之Android常见命令操作及一些普通布局属性
  2. Android共有五种布局方式
  3. Android布局_View属性
  4. Android利用setLayoutParams在代码中调整布局(Margin和居中)
  5. Android界面布局基本属性
  6. android布局动画实现方法
  7. 安卓入门.RelativeLayout相对布局2
  8. Android TabHost的标签放在底部,已写成框架,继承即可

随机推荐

  1. 抛砖引玉,谈谈Android移植到现有硬件平台
  2. mysql横向转纵向、纵向转横向排列的方法
  3. navicat创建MySql定时任务的方法详解
  4. MySql比较运算符正则式匹配REGEXP的详细
  5. 记一次MySQL的优化案例
  6. 谈谈MySQL中的隐式转换
  7. 详解MySQL 8.0 之不可见索引
  8. 实例验证MySQL|update字段为相同的值是否
  9. mysql 8.0.22 安装配置方法图文教程
  10. 详解mysql 中的锁结构