为什么 Android 主题不适用于单个按钮

Why Android theme is not applying to a single button

我想使用 styles.xml 为布局中的按钮设置样式,如下所示。

styles.xml

<resources>

<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
</style>

<style name="SearchButton" parent="Widget.AppCompat.Button.Colored">
    <item name="android:textColor">@color/white</item>
    <item name="android:textSize">15sp</item>
    <item name="colorButtonNormal">@color/colorPrimary</item>
</style>

并使用 android:theme 将此样式应用于按钮。

<Button
    android:theme="@style/SearchButton"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_margin="15dp"
    android:text="@string/search_button_text" />

在预览 window 中,此代码未对默认的灰色 Material 设计按钮进行任何更改。

额外:我试过使用 "style=",它会部分工作,但它会忽略 colorButtonNormal 并改用 accentColor。

彩色按钮采用 styles.xml 文件中 colorAccent 属性的颜色。 将 colorAccent 的颜色更改为您选择的颜色以获得所需的背景颜色。

colorButtonNormal 仅适用于具有默认样式的按钮。

你可以试试这个answer