【问题标题】:Adjust view's position on other view's visibility in Relative Layout?在相对布局中调整视图在其他视图可见性上的位置?
【发布时间】:2015-05-26 14:48:51
【问题描述】:

我可以只用 xml 来做吗:

  1. 相对布局中的 View-A 与某些 View-B 对齐时, 可见的。
  2. 在相对布局中的 View-A 与某些 View-C 对齐时 视图 -B 不见了??

考虑 B 之上的 C 和 A 之上的 B :

【问题讨论】:

  • 请清楚,否则发布你的代码
  • 请发布您的代码\
  • 我为你的例子更新了我的答案。

标签: android android-layout


【解决方案1】:

您不能直接在 xml 中这样做,但根据您的需要,您可以使用layout_alignWithParentIfMissing

例如,如果 View-A 与 View-B 左对齐并且 View-B 可见性消失,则 View-A 将与其相对布局父级左对齐。

看下一个例子:

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <View
        android:id="@+id/viewB"
        android:layout_width="20dp"
        android:layout_height="match_parent"
        android:layout_alignParentRight="true" />

    <View
        android:id="@+id/viewA"
        android:layout_width="20dp"
        android:layout_height="match_parent"
        android:layout_toLeftOf="@id/viewB"
        android:layout_alignWithParentIfMissing="true" />

</RelativeLayout>

如果 viewB 可见,则 viewB 将位于 RelativeLayout 的右侧,而 viewA 将位于其左侧。如果 viewB 消失了,viewA 将在 RelativeLayout 的右侧。

没有这一行:

android:layout_alignWithParentIfMissing="true" />

viewB消失后viewA会显示在RelativeLayout左侧...

对于您的示例,您只需将 layout_toLeftOf 属性替换为 layout_above。

否则,您需要在您的活动(或片段,或任何地方......)中以编程方式更改 View-A 或 View-B 对齐方式

【讨论】:

    【解决方案2】:

    我假设顺序是 B 左侧的 A(如果 B 不可见,则为 C),但如果您颠倒顺序,同样的逻辑也适用。假设您可以执行以下操作:

    • 使 B 与 A 的右侧对齐
    • 使 A 与 C 的左侧对齐

    这样,当 B 可见时,它将在 A 的右侧,这意味着 A 与 B 和 C 的左侧对齐。当 B 消失时,第一个条件消失了,因此 A 仅对齐在 C 的左边。

    【讨论】:

      猜你喜欢
      • 2017-10-30
      • 1970-01-01
      • 1970-01-01
      • 2014-02-22
      • 2013-11-15
      • 1970-01-01
      • 2019-04-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多