无论布局结构如何,按钮小部件都绘制在任何其他小部件之上。当应用 Material Dark/Light 主题时,它会在 RelativeLayout 和 FrameLayout 中重复。请查看下面的屏幕截图,以更好地说明这种奇怪的行为。
检查了 Nexus 4 和 Nexus 5。但是我怀疑它与设备有关。
最佳答案
Android 5.0 Lollipop 和 Material Design 引入了新属性来指定小部件的高度(Z 索引)。据描述here .
要在按钮上绘制 View ,您可以将 android:elevation="1dp"
添加到 View
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Don't look so deep"
/>
<View
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#C00"
android:elevation="1dp"
/>
以下是早期对被误解问题的回答的一部分,保留以备将来引用
使用 RelativeLayout,您必须指定元素相对于其他元素的位置。
假设您想要在按钮下方设置 View
,则必须向元素添加 id 并指定 View 在按钮下方:
<Button
android:id="+id/myactivity_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Don't look so deep"
/>
<View
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#C00"
android:layout_below="@id/myactivity_button"
/>
查看 Android Developer Guide for RelativeLayout和 available LayoutParameters for RelativeLayouts
FrameLayout
通常不适合组织多个组件。 FrameLayout 旨在遮挡屏幕上的一个区域以显示单个项目。可以使用 android:layout_gravity
属性控制 FrameLayout 子元素的位置。
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="top"
android:text="Don't look so deep"
/>
<View
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#C00"
android:layout_gravity="bottom"
/>
查看 Android docs for FrameLayout和 available parameters for the layout_gravity
关于android - 应用 Material 主题时按钮的渲染顺序错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28105551/