Viewstub一旦膨胀就不会移动相邻的视图。解?

时间:2016-03-02 20:10:27

标签: android xml android-inflate viewstub

我在DrawerLayout中有一个ViewStub,我打算根据用户的需要动态地扩展和删除这个存根。

抽屉由RelativeLayout组成,包含: 1. TextView 2.提到的ViewStub 3. ListView

当我向ViewStub充气时,生成的视图不会取代ListView。相反,它在ListView下面膨胀,用户无法看到它。我希望存根在虚增时按下列表。

我该如何解决这个问题?

以下是XML代码:

<RelativeLayout

        android:id="@+id/drawer"
        android:layout_width="250dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:background="#dedede"
        android:fitsSystemWindows="true"
        android:orientation="vertical"
        >

        <TextView
            android:id="@+id/drawer_text"
            android:layout_width="match_parent"
            android:layout_height="100dp"
            android:background="@android:color/holo_red_light"/>

        <ViewStub
            android:id="@+id/info_stub"
            android:layout_width="match_parent"
            android:layout_height="100dp"
            android:layout="@layout/info_entry_stub"
            android:layout_below="@+id/drawer_text"/>

        <ListView
            android:id="@+id/drawernavlist"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@android:color/holo_orange_dark"
            android:layout_below="@+id/info_stub"
            android:layout_gravity="start">
        </ListView>

    </RelativeLayout>

存根指向的布局:

<?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:id="@+id/info_entry_stub">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@android:color/darker_gray"
        android:text="HELLO"/>


</RelativeLayout>

解决方案:我通过在ViewStub中添加属性来解决它:

android:inflatedId="@id/info_stub"

1 个答案:

答案 0 :(得分:0)

太晚了!但是对于那些可能遇到同样问题的人来说。

一旦您对ViewStub进行充气,它将从视图树层次结构中删除,那么其id将不会退出。因此,在"@+id/info_stub" id的布局中没有视图,并且您的ListView想要低于该值(不会退出)

您可以在LinearLayout中使用android:inflatedId="@+id/info_layout"或使用ViewStub,并将android:layout_below="@+id/info_layout"添加到ListView