`
baobaoupup
  • 浏览: 470676 次
文章分类
社区版块
存档分类
最新评论

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
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics