了解风格

时间:2018-12-15 17:05:09

标签: android button themes

我正在尝试为Android应用程序中的按钮设置自定义颜色,但为accentColor设置的值始终用作按钮背景色。我试图为所有按钮设置全局颜色,而不必分别为每个按钮指定样式或主题。

您能否解释一下样式在Android中的工作原理?我了解基本知识,但是覆盖现有主题对我而言永远无效:(

styles.xml

<resources>

<style name="AppTheme" parent="Theme.MaterialComponents.Light.NoActionBar">
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
    <item name="android:titleTextColor">#ffffff</item>
    <item name="android:buttonStyle">@style/AppTheme.Button</item>

</style>

<style name="AppTheme.NoActionBar">
    <item name="windowActionBar">false</item>
    <item name="windowNoTitle">true</item>
</style>

<style name="AppTheme.Button" >
    <item name="android:backgroundTint">@color/colorPrimaryDark</item>
</style>

<style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />

<style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />

2 个答案:

答案 0 :(得分:1)

  

简短的答案:尝试转到如何   Theme.MaterialComponents.Light.NoActionBar正在定义   按钮并覆盖这些属性。

Android样式通过合并主题中所有定义的属性来工作。例如,您有一个AppTheme,其中包括每个内置视图的主题。如果您为按钮创建一个新主题并定义了几个属性,则android studio要做的就是将AppTheme中定义的所有属性与新属性合并。因此,如果要覆盖按钮,则需要深入研究并找出在应用程序中设置的常规AppTheme中定义的确切属性是什么,

例如,这是我更改应用程序中所有按钮的颜色的方式,因为它们使用的是全局属性: 在常规的styles.xml文件中,我输入:

<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
    ...
    <item name="colorButtonNormal">?myCustomColor</item>
    ...
</style>

但是,如果我想更改按钮的backgroundTint属性,则必须执行以下操作:

<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
    ...
    <item name="colorButtonNormal">?myCustomColor</item>
    <item name="android:buttonStyle">@style/AppButtonStyle</item>
    ...
</style>

<style name="AppButtonStyle" parent="Widget.AppCompat.Button">
    <item name="android:backgroundTint">?attr/colorAccent</item>
</style>

此外,请访问this cool Medium post,以获取有关本主题的另一种开发人员经验。

答案 1 :(得分:0)

我设法解决了这个问题。这里要注意的主要点是我使用的是 Theme.MaterialComponents ,而不是AppCompact。

我已经定义了一种新样式,并将其设置为buttonStyle,这是指AppCompact按钮的样式。为了设置材质按钮的样式,我不得不使用“ materialButtonStyle”而不是“ buttonStyle”。

可以为MaterialComponents覆盖的所有相关属性可以在材料设计文档中找到:https://www.material.io/develop/android/components/material-button/

所以最终代码如下:

<resources>

    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.MaterialComponents.Light.NoActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
        <item name="colorSecondary">@color/colorAccent</item>
        <item name="floatingActionButtonStyle">@style/AppTheme.Test</item>
        <item name="materialButtonStyle">@style/AppTheme.Button</item>
        <!--<item name="buttonStyle">@style/Widget.MaterialComponents.Button.Icon</item>-->
    </style>

    <style name="AppTheme.Button" parent="@style/Widget.MaterialComponents.Button">
        <item name="android:backgroundTint">#D400EB</item>
    </style>
    <style name="AppTheme.Test" parent="@style/Widget.MaterialComponents.FloatingActionButton">
        <item name="backgroundTint">#A305B1</item>
    </style>

    <style name="AppTheme.NoActionBar">
        <item name="windowActionBar">false</item>
        <item name="windowNoTitle">true</item>
    </style>
    <style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar"/>
    <style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light"/>

</resources>