Android Layout Trick
来源:互联网 发布:微信淘宝客源码 编辑:程序博客网 时间:2024/06/05 08:14
滥用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
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
- Android Layout Trick
- Android Layout Trick #2: Include to Reuse
- Android Layout Trick #2: Include to Reuse
- Android的一些trick
- trick
- Trick
- trick
- android layout
- android layout
- Android Layout
- Android Layout
- Android Layout
- android layout
- android layout
- Android Layout
- Android Layout
- android : Layout
- android layout
- C#正则表达式整理备忘 6
- 28.5.1 安装和使用OEM
- C#正则表达式整理备忘 7
- 将小写金额转换为中文大写的SQL函数
- 图片滤镜demo
- Android Layout Trick
- 28.5.2 数据库性能
- LAMP架构
- 利用VS.NET快速开发WINDOWS服务
- 用 ThreadLocal 管理用户session
- 使用存储过程并返回值与及返回值的获得方法
- 饶雪漫经典语录
- 没有技巧的技巧
- 使用memcached实现session远程分布式存储