我有一个活动和很多小部件,其中一些有动画效果,因为这些动画效果,一些小部件会移动(平移)到另一个小部件的上面。例如,文本视图会移动到某些按钮上面……
现在的问题是,我希望按钮始终处于最前面。当文本视图移动时,我希望它在按钮的后面。
我无法实现这个功能,尝试了我所知道的所有方法,“bringToFront()”肯定行不通。
请注意,我不想通过布局中元素的放置顺序来控制z-order,因为我根本做不到:),布局很复杂,我无法将所有按钮都放在布局的开头。
yourView.bringToFront();
A / B / C
并想将 A
置于前面,所以运行 a.bringToFront()
后,我的布局变成了 B / C / A
。 - Ferran MaylinchLinearLayout
不能与bringToFront
一起使用。我最终使用了RelativeLayout
。请参见我在问题本身上的评论。 - Ferran MaylinchViewCompat.setTranslationZ(view, 1)
却完美地工作。 - Bimde使用此XML代码
android:translationZ="90dp"
我一直在Stack Overflow上寻找一个好的答案,当我找不到时,我去查阅了文档。
似乎还没有人碰巧找到这个简单的答案:
ViewCompat.setTranslationZ(view, translationZ);
默认翻译 z 为 0.0
setZ()
呢?如果我没记错的话,视图的 Z 位置实际上是其高度和 Z 轴平移量之和。 - Gensoukyou1337ViewCompat.setZ(view, yourValueInPixels);
view.setZ()
仅适用于API 21及更高版本。 - Johnny Five一个更简单的解决方案是编辑活动的XML。使用
android:translationZ=""
bringToFront()
是正确的方法,但是,请注意必须在您根视图下最高级别的视图上调用 bringToFront()
和 invalidate()
方法,例如:
您的视图层次结构如下:
-RelativeLayout
|--LinearLayout1
|------Button1
|------Button2
|------Button3
|--ImageView
|--LinearLayout2
|------Button4
|------Button5
|------Button6
所以,当你将按钮(1-6)还原为动画时,你的按钮会在ImageView
下方。要将其置于ImageView
上方,你必须在LinearLayout
上调用bringToFront()
和invalidate()
方法。然后它就可以工作了 :)
**注意:记得为你的根布局或动画视图的祖先布局设置android:clipChildren="false"
。让我们看一下我的实际代码:
.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>
一些在 .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);
祝你好运!
如果您正在使用ConstraintLayout
,只需将该元素放在其他元素之后即可使其位于其他元素的前面。
请尝试使用 FrameLayout
,它可以让你把视图放在彼此之上。你可以创建两个 LinearLayouts
,一个放置背景视图,另一个放置前景视图,然后使用 FrameLayout
来组合它们。希望这有帮助。
FrameLayout glFrame=(FrameLayout) findViewById(R.id.animatedView);
glFrame.addView(yourView);
glFrame.bringToFront();
glFrame.invalidate();
android:translationZ=""
可能还有另一种方法可以拯救情况。只需使用所需的布局初始化一个新的对话框,然后显示它即可。我需要将加载视图显示在DialogFragment上,这是我成功的唯一方法。
Dialog topDialog = new Dialog(this, android.R.style.Theme_Translucent_NoTitleBar);
topDialog.setContentView(R.layout.dialog_top);
topDialog.show();
bringToFront()在某些情况下可能无效,例如我所遇到的情况。但dialog_top布局的内容必须覆盖UI层上的任何内容。但无论如何,这是一种丑陋的解决方法。
RelativeLayout
,并在XML中将“顶部”视图放在最后。请参见https://dev59.com/7HLYa4cB1Zd3GeqPYHhc#31340762。 - Ferran Maylinch