我想在listview中每一个行里添加一个SlidingDrawer控件,当用户需要查看该行的详细信息时,可拖动SlidingDrawer的handle展开。现在碰到2个问题:1.在listview的item布局文件中,定义SlidingDrawer控件android:layout_height="wrap_content"时,会报错"SlidingDrawer cannot have UNSPECIFIED dimensions"。这个是因为没有为SlidingDrawer定义高度,虽然可以通过手工写个具体高度值(比如160dp)避免报这个错误,但我需要动态计算该SlidingDrawer高度。2.当在手工写个具体高度值时,即使SlidingDrawer控件未展开,系统也在listview的每一行中给它预留了那个高度的空间。能不能在拖动handle时,才展开相应高度,完全折叠时,不预留那段空间呢?请大家发表下看法,谢谢了~
<?xml version="1.0" encoding="UTF-8"?><FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@color/bg_common_deep_dark"> <TextView android:id="@+id/sigin_status_textview"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:textColor="@color/white"
android:theme="@android:style/Theme.Translucent"
android:background="@drawable/loading_sccuess"/>
<SlidingDrawer
android:id="@+id/sliding_Notification"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:handle="@+id/notification_alert"
android:layout_gravity="bottom"
android:content="@+id/notification_content">
<LinearLayout
android:id="@id/notification_alert"
style="@style/toolbar"
android:layout_height="40dip"
android:layout_width="fill_parent"
android:layout_alignParentBottom="true"
android:gravity="center"
android:background="@drawable/bg_homescreen_notification_bar">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:textColor="@color/white"
android:text="@string/notification"/>
<TextView
android:id="@+id/unread_notification_count"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone"
android:textStyle="bold"
android:gravity="center"
android:background="@drawable/nov_bubble_bg"/>
</LinearLayout>
<ListView android:id="@id/notification_content"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
</ListView>
</SlidingDrawer></FrameLayout>
你好,我照着你说的改了下,还是报"SlidingDrawer cannot have UNSPECIFIED dimensions"的错误。只能麻烦你看下了,谢谢。main.xml<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ListView
android:id="@+id/main_lv"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
</LinearLayout>
listview的item布局
<?xml version="1.0" encoding="UTF-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView android:id="@+id/d_tv"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="此处显示listview行内容"
android:textColor="#ffffff"/>
<SlidingDrawer
android:id="@+id/slidingdrawer"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:handle="@+id/handle"
android:layout_gravity="bottom"
android:content="@+id/content">
<ImageButton android:id="@+id/handle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/icon"/>
<LinearLayout
android:id="@+id/content"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView android:text="此处显示SlidingDrawer展开后的内容"
android:id="@+id/TextView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
</SlidingDrawer>
</FrameLayout>
在SlidingDrawer 的onMeasure 函数里,如果不设置固定值,传进来的高度永远是0,导致heightSpecMode被设定为UNSPECIFIED 。UNSPECIFIED 在google的官方解释为:UNSPECIFIED: This is used by a parent to determine the desired dimension of a child view. For example, a LinearLayout may call measure() on its child with the height set to UNSPECIFIED and a width of EXACTLY 240 to find out how tall the child view wants to be given a width of 240 pixels. 貌似listView 的item 需要知道子view到底有多高,然后它再计算自己有多高。所以就用了UNSPECIFIED。还有在listview中使用SlidingDrawer,不太好,SlidingDrawer需要捕获 滑动的动作,listview也需要捕获滑动的动作,这样照成给用户的体验很差。