当图层可见性改变时刷新图层



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

最初R2被设置为可见性(消失),这样列表就位于编辑框的顶部。然而,当我按下B1按钮时,我希望R2是可见的,并且列表现在移动到R2之上。

在xml设置中,列表被设置在R2之上:android:layout_above="@id/R2Layout"但是由于R2最初被设置为gone,因此列表位于编辑框的顶部。那好。

我的问题是,一旦按钮B1被按下,R2变得可见,但列表不使空间R2和R2以上移动。R2和list相互重叠。

我试图通过执行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>

您可以将父布局更改为LinearLayout,并可以将ButtonEditText包裹在Horizontal 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>

最新更新