【问题标题】:Android Slide a view down when another view's visibility is changed from gone to visibleAndroid 当另一个视图的可见性从消失变为可见时向下滑动视图
【发布时间】:2015-12-12 01:51:06
【问题描述】:

我有一个视图,其可见性设置为大部分时间(视图 A)和它下方的另一个视图始终可见(视图 b),有时视图 A 视图在某些情况下设置为可见,发生这种情况时,视图 B 将不可见(主要是因为视图 ​​A 在出现时阻止了它)。我想知道系统是否有办法在视图 B 出现时将视图 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>

【问题讨论】:

    标签: android view visibility


    【解决方案1】:

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

    <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>
    

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

    【讨论】:

      【解决方案2】:

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

      【讨论】:

        【解决方案3】:

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

        【讨论】:

          猜你喜欢
          • 2017-10-24
          • 1970-01-01
          • 1970-01-01
          • 2018-07-15
          • 1970-01-01
          • 1970-01-01
          • 2015-04-17
          • 2014-10-05
          • 1970-01-01
          相关资源
          最近更新 更多