【问题标题】:Overlapping views with one VISIBILITY set to GONE causes other views to disappear一个 VISIBILITY 设置为 GONE 的重叠视图会导致其他视图消失
【发布时间】:2014-01-11 14:59:59
【问题描述】:

我有一个包含 3 个 ImageView(A、B 和 C)的 RelativeLayout。当其中一个被点击时,另外两个会淡出,点击的图像会滑到A所在的位置。如果点击的图像是A,那么另外两个就会淡出。图像完成滑动动画后,其他图像可见性属性设置为 GONE。

当点击的图像是 A 时,这完全符合我的预期。但是当点击的图像是 B 或 C(现在与 A 占用相同的空间)时,将 A 的可见性设置为 GONE 会导致点击的图像也消失(如,我看不到它)。

如果我将未点击图像的可见性设置为 INVISIBLE 而不是 GONE,则没有问题。我还应该提到,如果单击 A,也没有问题。

因此,问题似乎是如果两个视图重叠,其中一个视图的可见性设置为 VISIBLE,另一个设置为 GONE,那么您将无法看到其中任何一个。为什么会这样?在再次标记为可见之前,具有可见性 GONE 的视图所占用的空间是否本质上是不可用的屏幕空间?我确信我可以删除视图而不是将它们设置为 GONE,但我不希望这样做。在我希望它们消失后不久,我会重新激活它们。

编辑:
我已将动画代码更改为使用 ObjectAnimator,以防问题是视图实际上没有正确翻译。它没有解决问题,在我将其他视图的可见性设置为 GONE 后,翻译后的视图仍然消失。即使我删除其他视图,而不是将它们的可见性设置为 GONE(例如使用 rootLayout.removeView(view)),问题实际上仍然存在。

这是布局 XML:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:ignore="ContentDescription,PxUsage">

    <ImageView
        android:id="@+id/image_A"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="image_spacing"
        android:layout_width="@dimen/image_width"
        android:layout_height="@dimen/image_height" />

    <ImageView 
        android:id="@+id/image_B"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="@dimen/image_spacing"
        android:layout_toRightOf="@id/image_A"
        android:layout_width="@dimen/image_width"
        android:layout_height="@dimen/image_height" />

    <ImageView 
        android:id="@+id/image_C"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="@dimen/image_spacing"
        android:layout_toRightOf="@id/image_B"
        android:layout_width="@dimen/image_width"
        android:layout_height="@dimen/image_height" />

</RelativeLayout>

...还有一些代码:

image.animate()
    .translationXBy(slideDistance)
    .setDuration(slideDuration)
    .setListener(new AnimatorListenerAdapter() {
        @Override
        public void onAnimationEnd(Animator animation) {
            for (int i=0; i<rootLayout.getChildCount(); i++) {
                if (i == selectedImage) continue;
                rootLayout.getChildAt(i).setVisibility(View.GONE);
            }
        }
    });

【问题讨论】:

  • 我还没有找到解决方案,但作为一种解决方法,我只是将未点击视图的可见性设置为不可见。这并不理想,但它可以满足我的需要。

标签: android android-layout android-view android-xml


【解决方案1】:

问题在于android:layout_toRightOf 布局属性。当目标视图消失时,布局规则并不真正知道该视图的放置位置。

如果缺少另一个布局规则的目标视图,您可以尝试使用 layout_alignWithParentIfMissing="true" 使视图与父相对布局对齐。

或者,如果它与INVISIBLE 一起使用,您也可以使用它。

【讨论】:

  • 我在每张图片中都添加了 alignWithParentIfMissing="true",但这并没有什么不同(不过这似乎是一个很好的逻辑)。我真的不想使用 INVISIBLE,因为一旦滑动动画完成,我想用其他一些视图填充 RelativeLayout,并且我不希望不可见视图干扰或导致任何问题。另外,它们会对布局做出不必要的贡献。
猜你喜欢
  • 2015-03-06
  • 2011-06-11
  • 1970-01-01
  • 2021-01-05
  • 1970-01-01
  • 2021-05-01
  • 1970-01-01
  • 2020-07-01
相关资源
最近更新 更多