Android 线性布局(LinearLayout)性能相关

太阳火神的美丽人生 (http://blog.csdn.net/opengl_es)

本文遵循“署名-非商业用途-保持一致”创作公用协议

转载请保留此句:太阳火神的美丽人生 - 本博客专注于敏捷开发及移动和物联设备研究:iOS、Android、Html5、Arduino、pcDuino否则,出自本博客的文章拒绝转载或再转载,谢谢合作。


如下嵌套线性布局中有两处性能问题,在不使用 Eclipse Adt 提示的情况下,你能找得出来吗?

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:paddingBottom="@dimen/activity_vertical_margin"    android:paddingLeft="@dimen/activity_horizontal_margin"    android:paddingRight="@dimen/activity_horizontal_margin"    android:paddingTop="@dimen/activity_vertical_margin"    tools:context="com.mitest.linearlayoutexample.MainActivity" >    <TextView        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_weight="1"        android:text="@string/hello_world" />    <LinearLayout        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_id="@+id/testLinearLayout02"        android:layout_weight="1" >        <TextView            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:layout_weight="1"            android:text="@string/hello_world" />    </LinearLayout></LinearLayout>

下面揭晓答案:

第一处:


使用布局宽度值 0dp 替换 wrap_content 会得到更好的性能;


第二处:


嵌套使用权重对性能有损害。

线性布局里面嵌套了一层线性布局,并且两层都使用了 weight 权重属性来分配布局内空间,如果出现这种情况,那么可能是界面布局考虑欠妥,我做的例子中,通过相对布局很好地解决了这个问题。

简而言之,能用相对布局替换的线性布局,应该就是可以避勉使用的线性布局,略有武断,但对未来扩展会留出很大空间,并且每一次界面布局的改变,最好都重新构思各种布局方式的使用。性能无小事,尤其对于 Android !




更多相关文章

  1. 记录-解决设置透明状态栏,软键盘弹起问题
  2. Android(安卓)Notes 之 Tween动画 (3)添加布局动画
  3. Android(安卓)fragment
  4. Android(安卓)base-adapter-helper 源码分析与扩展
  5. android:weight属性的使用——android开发之xml布局文件
  6. Android横向滚动屏幕特效分析
  7. Android(安卓)中gradient的用法
  8. android中的对话框之三:自定义对话框
  9. Android的FrameLayout使用要注意的问题

随机推荐

  1. Android(安卓)判断网络连接
  2. android 学习笔记(二)
  3. 主界面监听返回键,退出程序
  4. 隐藏android中EditText的下划线
  5. Flutter实现android应用内版本更新功能
  6. Android(安卓)字符串格式化 千位符
  7. Java中的instanceof关键字在Android中的
  8. android客户端发送XML数据至服务器
  9. android获取文件目录
  10. android 使用VideoView加载raw目录内视频