android - 应用 Material 主题时按钮的渲染顺序错误

标签 android android-layout android-theme

无论布局结构如何,按钮小部件都绘制在任何其他小部件之上。当应用 Material Dark/Light 主题时,它会在 RelativeLayout 和 FrameLayout 中重复。请查看下面的屏幕截图,以更好地说明这种奇怪的行为。

检查了 Nexus 4 和 Nexus 5。但是我怀疑它与设备有关。

How it looks in Android Studio

How it looks on my Nexus 4

最佳答案

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 RelativeLayoutavailable 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 FrameLayoutavailable parameters for the layout_gravity

关于android - 应用 Material 主题时按钮的渲染顺序错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28105551/

相关文章:

android - 更改对话框的背景暗色出现在 android 中

Android API 级别 30 setSystemBarsAppearance 不会覆盖主题数据

Android:无法解析 appcompat-v7 主题

java - onCreateContextMenu 未被调用

android - 在 RecyclerView 下方显示 LinearLayout

android - 使布局在android中处于非 Activity 状态

android - 堆转储中的奇怪内容

java - 安卓/SQL服务器: Set Text to "No time" if the data at database is NULL

android - 不幸的是 "App"已经停止了 Android Studio

android - 在 Android 中设置锁屏背景(就像 Spotify 一样)