当前位置: 代码迷 >> Android >> Android 线性布局(LinearLayout)性能有关
  详细解决方案

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

热度:189   发布时间:2016-04-28 00:31:24.0
Android 线性布局(LinearLayout)性能相关

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 !




  相关解决方案