0

我有一个相对布局(RV1),其中顶部是列表视图。在此之下,我有另一个包含图像和按钮的相对视图 (R2)。最后在 R2 下方,在 R1 的底部有一个编辑框和一个按钮 (B1)。

最初,R2 设置为 visibility(gone),以便列表位于编辑框的顶部。但是,当我按下按钮 B1 时,我希望 R2 可见并且列表现在移动到 R2 上方。

在 xml 设置中,列表设置在 R2 上方: android:layout_above="@id/R2Layout" 但由于 R2 最初设置为消失,因此列表位于编辑框的顶部。没关系。

我的问题是,一旦按下按钮 B1,R2 就会变得可见,但列表不会为 R2 腾出空间并移动到 R2 上方。R2 和列表相互重叠。

我试图通过执行 view.invalidate() 使整个视图自行刷新,但它不起作用。

我该如何解决这个问题?

下面是xml代码。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/r1"
    android:layout_width="match_parent"
    android:layout_height="fill_parent" >

<EditText
    android:id="@+id/et"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:layout_marginBottom="5dp"
    android:layout_marginLeft="5dp"
    android:layout_toLeftOf="@+id/b1"
    android:padding="5dp"
    android:paddingTop="10dp" >

</EditText>

<RelativeLayout
    android:id="@+id/r2"
    android:visibility="gone"
    android:layout_width="wrap_content"
    android:layout_margin="5dp"
    android:layout_height="wrap_content"
    android:layout_above="@+id/et"
    android:layout_alignParentLeft="true" >

    <ImageView
        android:id="@+id/iv"
        android:visibility="gone"
        android:layout_width="80dp"
        android:layout_height="80dp" />

    <Button
        android:id="@+id/b2"
        android:visibility="gone"
        android:layout_marginLeft="5dp"
        android:layout_toRightOf="@+id/iv"
        android:layout_alignTop="@+id/iv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="button" /> 
</RelativeLayout>

<FrameLayout
    android:id="@+id/frameLayout1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_above="@id/r2"
    android:layout_marginBottom="10dp" >
    <ListView
        android:id="@+id/list"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:dividerHeight="2.0sp"
        android:fastScrollEnabled="true"
        android:smoothScrollbar="true"
        android:stackFromBottom="true" />

    <!-- Here is the view to show if the list is empty -->

    <LinearLayout
        android:id="@+id/empty"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical" >

        <!-- If the list is empty because there are no files... -->

        <TextView
            android:id="@+id/empty_text"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:gravity="center"
            android:visibility="invisible"
            android:text="List empty"
            android:textAppearance="?android:attr/textAppearanceMedium" />

    </LinearLayout>
</FrameLayout>

<Button
    android:id="@+id/b1"
    android:layout_width="50dip"
    android:layout_height="50dip"
    android:layout_alignParentBottom="true"
    android:layout_alignParentRight="true"
    android:layout_centerVertical="true"
    android:layout_marginRight="5dp" />

</RelativeLayout>
4

1 回答 1

0

您可以将您的父布局更改为并LinearLayout可以将您ButtonEditTextHorizontal Linear Layout

您的最终代码将如下所示:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/r1"
    android:layout_width="match_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <RelativeLayout
        android:id="@+id/r2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="5dp"
        android:layout_weight="1"
        android:visibility="gone" >

        <ImageView
            android:id="@+id/iv"
            android:layout_width="80dp"
            android:layout_height="80dp"
            android:visibility="gone" />

        <Button
            android:id="@+id/b2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignTop="@+id/iv"
            android:layout_marginLeft="5dp"
            android:layout_toRightOf="@+id/iv"
            android:text="button"
            android:visibility="gone" />
    </RelativeLayout>

    <FrameLayout
        android:id="@+id/frameLayout1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="10dp"
        android:layout_weight="1" >

        <ListView
            android:id="@+id/list"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:dividerHeight="2.0sp"
            android:fastScrollEnabled="true"
            android:smoothScrollbar="true"
            android:stackFromBottom="true" />

        <!-- Here is the view to show if the list is empty -->

        <LinearLayout
            android:id="@+id/empty"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:orientation="vertical" >

            <!-- If the list is empty because there are no files... -->

            <TextView
                android:id="@+id/empty_text"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:gravity="center"
                android:text="List empty"
                android:textAppearance="?android:attr/textAppearanceMedium"
                android:visibility="invisible" />
        </LinearLayout>
    </FrameLayout>

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

        <EditText
            android:id="@+id/et"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginBottom="5dp"
            android:layout_marginLeft="5dp"
            android:ems="10"
            android:padding="5dp"
            android:paddingTop="10dp" >

            <requestFocus />
        </EditText>

        <Button
            android:id="@+id/b1"
            android:layout_width="match_parent"
            android:layout_height="50dip"
            android:layout_marginRight="5dp" />
    </LinearLayout>

</LinearLayout>
于 2013-04-30T05:48:26.840 回答