我可以只用 xml 做这个吗:
- 相对布局中的 View-A 与某个 View-B 对齐时 可见的。
- 相对布局中的 View-A 与某些 View-C 对齐时 查看 -B 不见了 ??
考虑 B 之上的 C 和 A 之上的 B:
最佳答案
您不能直接在 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。
否则您需要在您的 Activity (或 fragment ,或任何地方......)中以编程方式更改 View-A 或 View-B 对齐方式
关于android - 在相对布局中调整 View 对其他 View 可见性的位置?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30393485/