Android 当另一个视图的可见性从消失变为可见时向下滑动一个视图

Android Slide a view down when another view's visibility is changed from gone to visible

我有一个视图的可见性设置为大部分时间都消失了(视图 A),而它下方的另一个视图始终可见(视图 b),有时视图 A 视图设置为从某些视图可见条件,当这种情况发生时,View B 将不可见(主要是因为 View A 在它出现时阻止了它)。我想知道是否有一种方法可以让系统在视图 B 出现时将其重新定位到视图 A 下方。

<RelativeLayout
    android:id="@+id/require_info"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
<LinearLayout
        android:id="@+id/payment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:visibility="gone">

        <View
            android:layout_width="fill_parent"
            android:layout_height="0.2dp"
            android:layout_marginTop="10dp"
            android:background="#c0c0c0" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="15dp"
            android:text="@string/quantity_label"
            android:textSize="10sp"
            android:textStyle="bold" />
</LinearLayout>
<LinearLayout
        android:id="@+id/contact_info"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/payment"
        android:orientation="vertical">

        <TextView
            android:id="@+id/contact_label"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Contact Information"
            android:textColor="#90CAF9"
            android:textSize="15sp" />
</LinearLayout>
</RelativeLayout>

如果 LinearLayout 中的 View B 上方有 View A,当您将 A 和 B 都设置为可见时,重新绘制布局,您将看到 View A 以上 View B。确保您的 LinearLayoutorientationvertical

您实际上需要将第一个 LinearLayout 的高度更改为 "wrap_content",目前设置为 "match_parent"。因此,如果您使视图 A 可见,它会获取屏幕上的全部可用 space 而您的视图 B 永远不会出现。

线性布局适合您的设计。以下示例显示了您如何做同样的事情。

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent" 
    android:orientation="vertical"
    >
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/linearLayout"
        android:orientation="vertical"
        android:visibility="visible"
        android:layout_weight="0.5"
        >
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="New Button1"
            android:id="@+id/button" />
    </LinearLayout>

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/linearLayout2"
        android:visibility="visible"
        android:layout_weight="0.5"
        >
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="New Button2"
            android:id="@+id/button2" />
    </LinearLayout>
</LinearLayout>

在上面,当您尝试使第一个线性布局可见并隐藏第二个线性布局时,可见的布局将全屏显示,反之亦然。如果将两个布局都设置为可见,则布局将正确可见(第一个布局及其下方的第二个布局)。