我有一個擴展視圖類定製的視圖。我想我的自定義視圖的兩個實例直接層疊在一起。我的佈局文件應該如何實現這個目標?如何層視圖
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