【问题标题】:Why Android theme is not applying to a single button为什么 Android 主题不适用于单个按钮
【发布时间】:2019-03-13 01:19:24
【问题描述】:

我想使用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" />

在预览窗口中,此代码不会对默认的灰色 Material Design 按钮进行任何更改。

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

【问题讨论】:

    标签: android android-button android-theme android-styles


    【解决方案1】:

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

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

    你可以试试这个answer

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-06-10
      • 2019-08-28
      • 1970-01-01
      • 1970-01-01
      • 2012-07-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多