【问题标题】:RelativeLayout and ViewStub inflationRelativeLayout 和 ViewStub 膨胀
【发布时间】:2012-10-24 08:34:00
【问题描述】:

我有以下布局。

<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 中呢?并把它放在中间

标签: android android-relativelayout inflate viewstub


【解决方案1】:

我知道这是一个老问题,但其中的技巧是将 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"/>

【讨论】:

  • 太棒了。你刚刚为我节省了几个小时 :)
  • 这确实有效。我想知道“inflatedId”的目的是什么。
  • 根据文档,ViewStub 用于在运行时膨胀布局。因此,当您在 ViewStub 上调用 inflate() 时,它会被引用的布局替换,而 ViewStub 会被膨胀的视图替换。如果你想在被充气后引用这个 ViewStub,你可以使用 inflatedId。
【解决方案2】:

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

【讨论】:

    【解决方案3】:

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

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

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

    2) 当您需要膨胀查看刷新其内容时,可以这样操作:

        View v = parentView.findViewById(R.id.view_stub_id);
        if (v instanceof ViewStub)
            v = ((ViewStub) v).inflate();
    
        // here v is always the inflated view
        // ...
    

    【讨论】:

      【解决方案4】:
      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>
      

      【讨论】:

      • 是的,这应该可以,但它是嵌套布局。在这种情况下,最好将 RelativeLayout 替换为 LinearLayout
      猜你喜欢
      • 2021-09-03
      • 1970-01-01
      • 1970-01-01
      • 2014-03-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多