今天在写listview中的item时,发现RelativeLayout中的子view的layout_height="match_parent"不管用。
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="@color/wheat" >
<TextView
android:id="@+id/title_drag_list_item"
android:layout_alignParentLeft="true"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_centerVertical="true"
android:gravity="center_vertical"
android:textSize="30sp"
/>
</RelativeLayout>
4.0.3的系统,搞不懂为啥
---------------------------------------------------
后来发现,其实不是RelativeLayout中的子view的layout_height="match_parent"不管用,而是relativeLayout的layout_height没起作用。
listview的item的高度是适应于item内部的容纳view的最大高度的,也就是说,他自己是无法设置高度的,这个类似于marginTop(或者bottom)不起作用。
而子view的match_parent又是适应于parent的最大高度,因此子view的高度成为了item内所有子view的最大高度,最终成为了大家都是wrap_parent。
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/wheat" >
<TextView
android:id="@+id/title_drag_list_item"
android:layout_alignParentLeft="true"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_centerVertical="true"
android:gravity="center_vertical"
android:textSize="30sp"
/>
<ImageView
android:id="@+id/sort_touch_area"
android:layout_alignParentRight="true"
android:layout_width="100dp"
android:layout_height="40dp"
android:layout_centerVertical="true"
android:background="@color/skyblue"
android:src="@drawable/liwei_icon_list_item_sort"
android:scaleType="centerInside"
android:contentDescription="for sort area"
/>
</RelativeLayout>
比如上面,Textview的高度最终也是40dp了。