【发布时间】: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