我有一个扩展视图类定制的视图。我想我的自定义视图的两个实例直接层叠在一起。我的布局文件应该如何实现这个目标?如何层视图
Q
如何层视图
16
A
回答
27
原来的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>
24
使用RelativeLayout
。 RelativeLayout
的后期子女将重叠较早的子女。
5
虽然这是真的,你可以实现使用RelativeLayout
和FrameLayout
,在API 21和更高的层次感..一切都变了。
在API 21或更高,XML的后面的孩子并不意味着它会覆盖前面的孩子。相反,它使用Elevation
来确定哪个视图将重叠其他视图(Z-Index)。
例如,如果你有这样的事情。
<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>
的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>
这样的View
将重叠Button
。 https://material.io/guidelines/material-design/elevation-shadows.html
相关问题
- 1. 视图层次
- 2. 如何从视图模型层将数据传递给视图层
- 3. 如何渲染服务层的视图?
- 4. 如何从MotionEvent获取底层视图?
- 5. Odoo10层次视图
- 6. iPhone视图分层
- 7. 如何在html视图中创建图层(或增加图层的z索引)?
- 8. UIView如何与Core Animation集成?图层托管或图层支持的视图?
- 9. 给定图层的对象。我如何获得iPhone中的图层视图?
- 10. 如何访问模型图层中的对象到视图图层
- 11. 如何渲染包含Core Animation图层的视图到位图?
- 12. 如何将GIf图像添加为视图上的图层?
- 13. 如何在当前视图上创建半透明视图图层?
- 14. 如何在graphviz中创建分层视图图形?
- 15. 如何从视图中删除最深的子图层?
- 16. 如何设计我的子视图“图层”?
- 17. 如何管理多个(不同的)视图图层
- 18. 如何在视图图层“单元格”无效“阴影路径?
- 19. 我如何在单个视图中设计这个图层
- 20. Flex AdvancedDataGrid层次视图itemrenderer
- 21. 片段的视图层次
- 22. 在UIKit视图混淆层
- 23. 反应原生视图层
- 24. 在SurfaceView上分层视图
- 25. 分层网页视图
- 26. 分层树视图实现
- 27. 多层次WPF视图
- 28. 视图层中的时区
- 29. 创建分层视图
- 30. ASP.NET - 分层网格视图
感谢队友:上 海拔&阴影
更多信息。我对此很感兴趣,而且这似乎工作得很好。我认为FrameLayout一次只能容纳1个可见的子视图......我已经在尝试创建自己的布局,并且像以下这样思考:必须有一个更简单的方法! P.S. 哦,如果不是很明显,观点默认情况下,从顶部呈现为底部,所以board2大干快上的委员会1顶部排出。 – Timo 2011-07-08 07:20:50
非常好。我不得不说,我不喜欢相对布局的使用频率,并且很高兴这个不使用相关布局。 (亲戚们很混乱!每一个对象都需要有一些属性来描述它的位置!这看起来会破坏使用布局的目的......) – ArtOfWarfare 2012-12-07 21:41:36