12

我有以下布局。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            style="@style/list_item_bottom">

    <TextView android:id="@+id/list_item_name"
              android:layout_width="match_parent"
              android:layout_height="wrap_content"/>

    <ViewStub android:id="@+id/stub_for_alt_name"
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:layout_below="@+id/list_item_name"
              android:layout="@layout/text_view_alt_name"/>

    <ImageView android:id="@+id/list_item_dopinfo1_image"
               android:layout_width="wrap_content"
               android:layout_height="wrap_content"
               android:layout_below="@+id/stub_for_alt_name"
               android:src="@drawable/ic_released"/>

</RelativeLayout>

我希望 ViewStub 低于 TextView 和 ImageView 低于 ViewStub。

ViewStub 膨胀的元素按我的预期显示。但是没有 ViewStub 的元素的 TextView 与 ImageView 重叠。

我的布局有什么问题?

更新:

我发现的唯一解决方案是给 ViewStub 和相关的 TextView 相同的android:id值。

4

4 回答 4

27

我知道这是一个老问题,但其中的技巧是将inflatedId参数设置为与实际 viewStub 本身相同,如下所示:

<ViewStub android:id="@+id/stub_for_alt_name"
          android:inflatedId="@id/stub_for_alt_name"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:layout_below="@+id/list_item_name"
          android:layout="@layout/text_view_alt_name"/>
于 2013-06-20T13:43:25.380 回答
5

我发现的唯一解决方案是给 ViewStub 和相关的 TextView 相同的android:id值。

于 2012-10-29T09:13:54.147 回答
3

这是一个老问题,但我有一个有用的补充

1)是的,就像其他人发布的一样 - 使用相同id的和inflatedId

<ViewStub 
   android:id="@+id/view_stub_id"
   android:inflatedId="@id/view_stub_id"
   ... 
/>

2) When you need to inflate view or refresh its content it's convenient to act like this:

    View v = parentView.findViewById(R.id.view_stub_id);
    if (v instanceof ViewStub)
        v = ((ViewStub) v).inflate();

    // here v is always the inflated view
    // ...
于 2015-01-24T20:38:07.407 回答
1
Hi you can try this one.

<RelativeLayout 
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        style="@style/list_item_bottom">

//you can add another relative layout containing your textview and imageview
  <RelativeLayout
        android:id="@+id/layout_content" 
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >


        <TextView android:id="@+id/list_item_name"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

         <ImageView android:id="@+id/list_item_dopinfo1_image"
             android:layout_width="wrap_content"
             android:layout_height="wrap_content"
             android:layout_below="@+id/stub_for_alt_name"
             android:src="@drawable/ic_released"
             android:layout_below="@+id/list_item_name" />

</RelativeLayout>

<ViewStub android:id="@+id/stub_for_alt_name"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:layout_below="@+id/list_item_name"
          android:layout="@layout/text_view_alt_name"
          android:layout_below="@+id/layout_content" />

</RelativeLayout>
于 2012-10-24T09:09:03.950 回答