Android 如何分层视图
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2629940/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to layer views
提问by Finer Recliner
I have a custom-made view that extends the View class. I would like 2 instances of my custom view layered directly on top of each other. How should my layout file look to achieve this?
我有一个扩展 View 类的定制视图。我想要我的自定义视图的 2 个实例直接层叠在一起。我的布局文件应该如何实现这一目标?
回答by Finer Recliner
Turns out FrameLayout was what I wanted. Just do this in your layout:
原来 FrameLayout 正是我想要的。只需在您的布局中执行此操作:
<FrameLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<com.proj.MyView
android:id="@+id/board1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<com.proj.MyView
android:id="@+id/board2"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</FrameLayout>
回答by CommonsWare
Use a RelativeLayout
. Later children of the RelativeLayout
will overlap earlier children.
使用一个RelativeLayout
. 的较晚孩子RelativeLayout
将与较早孩子重叠。
回答by aldok
While it's true that you can achieve layering using RelativeLayout
and FrameLayout
, in API 21 and higher.. things have changed.
虽然您确实可以使用RelativeLayout
and实现分层FrameLayout
,但在 API 21 及更高版本中……情况发生了变化。
In API 21 and higher, the later children of xml doesn't mean it will overlap earlier children. Instead, it uses Elevation
to determine which view will overlap other view (Z-Index).
在 API 21 及更高版本中,xml 的较晚子代并不意味着它将与较早的子代重叠。相反,它用于Elevation
确定哪个视图将与其他视图重叠(Z-Index)。
For example, if you have something like this.
例如,如果你有这样的事情。
<FrameLayout
android:layout_height="match_parent"
android:layout_width="match_parent">
<Button
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/white"
android:text="Hello World"
android:layout_gravity="top" />
<View
android:layout_width="match_parent"
android:layout_height="10dp"
android:background="@android:color/black"
android:layout_gravity="bottom" />
</FrameLayout>
The View
will not be visible even though it was added after Button
. That's because Button
has higher elevation than View
. To rearrange Z-Index you can add elevation
attribute to respective views.
该View
不会是即使它之后添加可见Button
。那是因为Button
海拔高于View
. 要重新排列 Z-Index,您可以elevation
向相应的视图添加属性。
<FrameLayout
android:layout_height="match_parent"
android:layout_width="match_parent">
<Button
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/white"
android:text="Hello World"
android:layout_gravity="top"
android:elevation="0dp" />
<View
android:layout_width="match_parent"
android:layout_height="10dp"
android:background="@android:color/black"
android:layout_gravity="bottom"
android:elevation="2dp" />
</FrameLayout>
This way the View
will overlap the Button
.
这样View
将重叠Button
.
More Information on Elevation & Shadows: https://material.io/guidelines/material-design/elevation-shadows.html
有关高程和阴影的更多信息:https: //material.io/guidelines/material-design/elevation-shadows.html