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 !




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值