Android Layout Trick

滥用LAYOUT会导致initialization, layout and drawing become slower.
如果你在嵌套几个linearlayout时用到weight参数,他要求孩子被测量俩次,这尤其昂贵的。
在一个listview中,你假设让他的ITEM显示出下列的一种格式。
< LinearLayout xmlns:android = "http://schemas.android.com/apk/res/android"
   
android:layout_width = "fill_parent"
   
android:layout_height = "?android:attr/listPreferredItemHeight"

   
android:padding = "6dip" >

   
< ImageView
       
android:id = "@+id/icon"

       
android:layout_width = "wrap_content"
       
android:layout_height = "fill_parent"
       
android:layout_marginRight = "6dip"

       
android:src = "@drawable/icon" />

   
< LinearLayout
       
android:orientation = "vertical"

       
android:layout_width = "0dip"
       
android:layout_weight = "1"
       
android:layout_height = "fill_parent" >

       
< TextView
           
android:layout_width = "fill_parent"
           
android:layout_height = "0dip"
           
android:layout_weight = "1"

           
android:gravity = "center_vertical"
           
android:text = "My Application" />

       
< TextView
           
android:layout_width = "fill_parent"
           
android:layout_height = "0dip"
           
android:layout_weight = "1"

           
android:singleLine = "true"
           
android:ellipsize = "marquee"
           
android:text = "Simple application that shows how to use RelativeLayout" />

   
</ LinearLayout >

</ LinearLayout >

这个layout可以工作但是非常浪费,因为你对这个LISTVIEW的每一个list view都要实例化这么一大串。可以relativelayout重写。
< RelativeLayout xmlns:android = "http://schemas.android.com/apk/res/android"
   
android:layout_width = "fill_parent"
   
android:layout_height = "?android:attr/listPreferredItemHeight"

   
android:padding = "6dip" >

   
< ImageView
       
android:id = "@+id/icon"

       
android:layout_width = "wrap_content"
       
android:layout_height = "fill_parent"

       
android:layout_alignParentTop = "true"
       
android:layout_alignParentBottom = "true"
       
android:layout_marginRight = "6dip"

       
android:src = "@drawable/icon" />

   
< TextView
       
android:id = "@+id/secondLine"

       
android:layout_width = "fill_parent"
       
android:layout_height = "26dip"

       
android:layout_toRightOf = "@id/icon"
       
android:layout_alignParentBottom = "true"
       
android:layout_alignParentRight = "true"

       
android:singleLine = "true"
       
android:ellipsize = "marquee"
       
android:text = "Simple application that shows how to use RelativeLayout" />

   
< TextView
       
android:layout_width = "fill_parent"
       
android:layout_height = "wrap_content"

       
android:layout_toRightOf = "@id/icon"
       
android:layout_alignParentRight = "true"
       
android:layout_alignParentTop = "true"
       
android:layout_above = "@id/secondLine"
       
android:layout_alignWithParentIfMissing = "true"

       
android:gravity = "center_vertical"
       
android:text = "My Application" />

</ RelativeLayout >

list item要显示的文字有俩行,如果其中一行不可见,application将简单的设置这个textview to GONE.
这个工作当用linealayout时表现很好,但是用relativelayout时则不行。
To solve this problem, you can use a very special layout parameter called alignWithParentIfMissing .

参考:http://www.curious-creature.org/2009/02/22/android-layout-tricks-1/
通过HierarchyViewer你可以看出俩中形式,第二个每次创建一个list item时都会少创建一linearlayout
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值