相对布局和视图存根的填充

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值。


为什么不将它放在一个LinearLayout中?然后将其放置在中间。 - Hossein Shahdoost
4个回答

27

我知道这是一个有点老的问题,但其中的诀窍是将inflatedId参数设置为与实际的viewStub本身相同,就像这样:

我知道这个问题有些陈旧,但其中的窍门是将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"/>

太棒了。你刚刚帮我节省了几个小时 :) - Konrad Morawski
这实际上起作用了。我想知道“inflatedId”的目的是什么。 - android developer
根据文档,ViewStub 用于在运行时填充布局。因此,当您在 ViewStub 上调用 inflate() 方法时,它会被引用的布局替换,并且 ViewStub 会被填充后的视图替换。如果您希望在充气后引用此 ViewStub,则可以使用 inflatedId。 - Ishaan

5
我找到的唯一解决方案是将ViewStub和相关的TextView赋予相同的android:id值。

3

It's an old question, but I've got a useful addition

1) Yes, like others have posted - use same id and 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
    // ...


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>

是的,这应该可以工作,但它是嵌套布局。在这种情况下,最好用LinearLayout替换RelativeLayout。 - AHTOH

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接