如何在一切事物面前展现观点?

Luk*_*kap 115 animation android button z-order textview

我有活动和很多小部件,其中一些有动画,而且由于动画,一些小部件正在移动(翻译)一个在另一个上.例如,文本视图正在移动某些按钮...

现在问题是我希望按钮始终在前面.当textview移动时,我想移动按钮.

我无法实现这一点我尝试了我所知道的一切,并且"bringToFront()"definitelly不起作用.

注意我不想按照放置元素的顺序来控制z顺序因为我根本不能:),布局很复杂而且我不能将所有按钮放在布局的乞讨处

Med*_*aly 129

您可以在要在前面看到的视图上调用bringToFront()

这是一个例子:

    yourView.bringToFront();
Run Code Online (Sandbox Code Playgroud)

  • 出乎意料的结果:我在垂直线性布局中使用了它,并且在底部出现了前面的视图...例如,我有'A/B/C`并且想要将'A`带到前面,所以在运行之后` a.bringToFront()`我最终得到了像'B/C/A`这样的布局. (3认同)
  • 所以我发现`LinearLayout`不能和`bringToFront`一起使用.我最终使用了`RelativeLayout`.请参阅我对问题本身的评论. (2认同)

shr*_*wdu 36

我一直在寻找堆栈溢出来找到一个好的答案,当我找不到一个我去浏览文档.

似乎没有人偶然发现这个简单的答案:

ViewCompat.setTranslationZ(view, translationZ);
Run Code Online (Sandbox Code Playgroud)

默认翻译z是0.0

  • @ Gensoukyou1337,然后`ViewCompat.setZ(view,yourValueInPixels);``view.setZ()`仅适用于API 21及更高版本. (2认同)

小智 21

更简单的解决方案是编辑活动的XML.使用

android:translationZ=""
Run Code Online (Sandbox Code Playgroud)

  • 这仅在 Android API 21 或更高版本中有用。 (4认同)

Jus*_*tin 19

bringToFront()是正确的方法,但是,请注意,您必须在最高级别视图(在根视图下)调用bringToFront()invalidate()方法,例如:

您的视图的层次结构是:

-RelativeLayout
|--LinearLayout1
|------Button1
|------Button2
|------Button3
|--ImageView
|--LinearLayout2
|------Button4
|------Button5
|------Button6
Run Code Online (Sandbox Code Playgroud)

因此,当您为按钮设置动画(1-> 6)时,按钮将位于(下方)的下方ImageView.为了将它翻转(上图)ImageView,你必须调用bringToFront(),并invalidate()在你的方法LinearLayout秒.然后它将工作:)**注意:请记住设置android:clipChildren="false"您的根布局或动画视图的gradparent_layout.我们来看看我的真实代码:

.XML

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:hw="http://schemas.android.com/apk/res-auto"
    android:id="@+id/layout_parent"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/common_theme_color"
    android:orientation="vertical" >

    <com.binh.helloworld.customviews.HWActionBar
        android:id="@+id/action_bar"
        android:layout_width="match_parent"
        android:layout_height="@dimen/dimen_actionbar_height"
        android:layout_alignParentTop="true"
        hw:titleText="@string/app_name" >
    </com.binh.helloworld.customviews.HWActionBar>

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@id/action_bar"
        android:clipChildren="false" >

        <LinearLayout
            android:id="@+id/layout_top"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:gravity="center_horizontal"
            android:orientation="horizontal" >
        </LinearLayout>

        <ImageView
            android:id="@+id/imgv_main"
            android:layout_width="@dimen/common_imgv_height"
            android:layout_height="@dimen/common_imgv_height"
            android:layout_centerInParent="true"
            android:contentDescription="@string/app_name"
            android:src="@drawable/ic_launcher" />

        <LinearLayout
            android:id="@+id/layout_bottom"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:gravity="center_horizontal"
            android:orientation="horizontal" >
        </LinearLayout>
    </RelativeLayout>

</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)

.java中的一些代码

private LinearLayout layoutTop, layoutBottom;
...
layoutTop = (LinearLayout) rootView.findViewById(R.id.layout_top);
layoutBottom = (LinearLayout) rootView.findViewById(R.id.layout_bottom);
...
//when animate back
//dragedView is my layoutTop's child view (i added programmatically) (like buttons in above example) 
dragedView.setVisibility(View.GONE);
layoutTop.bringToFront();
layoutTop.invalidate();
dragedView.startAnimation(animation); // TranslateAnimation
dragedView.setVisibility(View.VISIBLE);
Run Code Online (Sandbox Code Playgroud)

格鲁克!


Ego*_*gor 15

试试吧FrameLayout,它让你可以把视图放在另一个上面.您可以创建两个LinearLayouts:一个具有背景视图,一个具有前景视图,并使用它们组合它们FrameLayout.希望这可以帮助.


Kri*_*hna 14

在xml中使用此代码

 android:translationZ="90dp"
Run Code Online (Sandbox Code Playgroud)

  • ‘90dp’从哪里来? (4认同)
  • 所需 API 21+ (2认同)
  • TranslationZ 值 (90dp) 添加到为 android:elevation 提供的值中。因此,平移Z + 高程将决定顺序。总和最高的视图将位于前面。其后面的下一个最高的依此类推。 (2认同)

小智 12

如果您正在使用ConstraintLayout,只需将元素放在其他元素之后,使其比其他元素排在前面


Mud*_*han 5

我遇到了同样的问题。以下解决方案对我有用。

 FrameLayout glFrame=(FrameLayout) findViewById(R.id.animatedView);
        glFrame.addView(yourView);
        glFrame.bringToFront();
        glFrame.invalidate();
Run Code Online (Sandbox Code Playgroud)

第二种解决方案是使用 xml 将此属性添加到视图 xml

android:translationZ=""
Run Code Online (Sandbox Code Playgroud)


Ema*_*uel 5

您可以尝试使用bringChildToFront,您可以在Android 开发人员页面中检查此文档是否有帮助。