【问题标题】:Android translate animation: moved view is overlappedAndroid翻译动画:移动的视图重叠
【发布时间】:2013-03-28 12:05:34
【问题描述】:

我的Activity 中有两个TextViews(我们称它们为TV1 和TV2),它们都属于不同的RelativeLayouts。我创建了一个TranslateAnimation 将 TV1 从它的位置移动到 TV2 的位置。问题是,当 TV1 到达 TV2 的 RelativeLayout 时,它会在它下方(移动的 TextView 与 TV2 的布局重叠),我希望 TV1 位于此布局之上。有没有办法把它带到前面?

谢谢!

[编辑]

我添加了我的布局代码。我想将 timeText 移动到 numTimeValue。可以看到,timeText 是在 numTimeValue 之后声明的。另外,我试过这个:

mTimeTextView.bringToFront();
((View)mTimeTextView.getParent()).bringToFront();

它仍然重叠。

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/outer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <LinearLayout
        android:id="@+id/header"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:baselineAligned="false"
        android:orientation="vertical" >

        <LinearLayout
            android:id="@+id/pointsLayout"
            style="@style/ScoreBar"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:baselineAligned="false"
            android:orientation="horizontal" >

            <LinearLayout
                android:id="@+id/numCorrectLayout"
                android:layout_width="0dip"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:orientation="vertical" >

                <TextView
                    android:id="@+id/numCorrectTitle"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center_horizontal"
                    android:text="@string/num_correct_title"
                    android:textSize="16sp" />

                <TextView
                    android:id="@+id/numGuessed"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center_horizontal"
                    android:textSize="22sp" />
            </LinearLayout>

            <LinearLayout
                android:id="@+id/numTimeLayout"
                android:layout_width="0dip"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:orientation="vertical" >

                <TextView
                    android:id="@+id/numTimeTitle"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center_horizontal"
                    android:text="@string/num_time_title"
                    android:textSize="16sp" />

                <TextView
                    android:id="@+id/numTimeValue"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center_horizontal"
                    android:textSize="22sp" />
            </LinearLayout>
        </LinearLayout>

        <RelativeLayout
            android:id="@+id/timeLayout"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal" >

            <ProgressBar
                android:id="@+id/time_progbar"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_centerVertical="true"
                android:layout_marginRight="10dp"
                android:layout_toLeftOf="@+id/timeText"
                android:indeterminateDrawable="@android:drawable/progress_indeterminate_horizontal"
                android:indeterminateOnly="false"
                android:maxHeight="20dip"
                android:minHeight="20dip"
                android:padding="15dp"
                android:progressDrawable="@drawable/time_progressbar" />

            <TextView
                android:id="@+id/timeText"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentRight="true"
                android:layout_centerVertical="true"
                android:layout_marginRight="5dp"
                android:textSize="30sp" />
        </RelativeLayout>
    </LinearLayout>

    <ImageView
        android:id="@+id/movieImage"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/header"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="30dp"
        android:contentDescription="@string/movie_to_guess" />
</RelativeLayout>

[编辑2]

简化版:

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/game_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <LinearLayout
        android:id="@+id/pointsLayout"
        style="@style/ScoreBar"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:baselineAligned="false"
        android:orientation="horizontal" >

            <TextView
                android:id="@+id/numTimeValue"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_horizontal"
                android:textSize="22sp" />
        <!--  </LinearLayout> -->
    </LinearLayout>

    <RelativeLayout
        android:id="@+id/timeLayout"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/pointsLayout"
        android:orientation="horizontal" >

        <ProgressBar
            android:id="@+id/time_progbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:layout_marginRight="10dp"
            android:layout_toLeftOf="@+id/timeText"
            android:indeterminateDrawable="@android:drawable/progress_indeterminate_horizontal"
            android:indeterminateOnly="false"
            android:maxHeight="20dip"
            android:minHeight="20dip"
            android:padding="15dp"
            android:progressDrawable="@drawable/time_progressbar" />

        <TextView
            android:id="@+id/timeText"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true"
            android:layout_marginRight="5dp"
            android:textSize="30sp" />
    </RelativeLayout>

    <ImageView
        android:id="@+id/movieImage"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/timeLayout"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="30dp"
        android:contentDescription="@string/movie_to_guess" />
</RelativeLayout>

【问题讨论】:

  • 这可能是由于第一个布局下第二个文本视图的布局。您可以在前面移动第二个布局或在第一个布局出现时隐藏第二个布局

标签: android translate


【解决方案1】:

这可能与各个布局元素的 z 顺序(即垂直堆叠)有关。两种解决方案:

(1) Change the z-order in your java code 以便将您想要在顶部的视图移到前面。在您的情况下,您将想要将包含您想要在顶部的TextView 的整个相对布局移动到前面。仅移动文本视图本身是行不通的。

(2)Control the z-order within the xml。默认情况下,在您的 XML 文件中向下添加的视图将在 z 顺序中更高。尝试交换相对布局的位置,看看它是否有效。

【讨论】:

  • 我编辑了我的帖子以添加我的布局。我尝试了这两件事,但它仍然是一样的。也许我做错了什么?
  • 您的内部 RelativeLayout 嵌套在 LinearLayout id = 标头中。我认为这是您遇到问题的地方。您可能需要解开这些布局以使其按您想要的方式工作。只看你是 XML,我不明白为什么你需要所有这些单独的嵌套 LinearLayout 字段 - 似乎你可以更有效地使用一两个相对布局。然后你会确定你将正确的布局移到前面。
  • 我添加了“标题”LinearLayout 以避免使用 android:layout_below,因为我认为这可能与我的问题有关。我用我的布局的简化版编辑了我的帖子,但情况还是一样。
  • 您试图移到前面的文本视图仍位于嵌套在另一个RelativeLayout 中的RelativeLayout 中。我认为这就是问题所在。看起来你只有 2 个主要布局块 - 你能把它们分开吗?也许在外部使用 LinearLayout 和 2 个相对布局?你需要将你想要移动到前面的东西与你想要在后面的东西分开。它不能嵌套在里面。
【解决方案2】:

尝试在您的 xml 中使用 android:elevation。

对于 TV1,提供 android:elevation = "1dp"

对于 TV2,提供 android:elevation = "0dp"

【讨论】: