Android 理解风格

Android 理解风格,android,button,themes,Android,Button,Themes,我试图在Android应用程序中为我的按钮设置自定义颜色,但accentColor的值设置始终用作按钮背景色。我试图为所有按钮设置全局颜色,而不必为每个按钮单独指定样式或主题 你能解释一下Android中的样式是如何工作的吗?我了解基本知识,但覆盖现有主题对我来说永远不起作用:( styles.xml <resources> <style name="AppTheme" parent="Theme.MaterialComponents.Light.NoActionBar"&g

我试图在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" />

@颜色/原色
@颜色/原色暗
@颜色/颜色重音
#ffffff
@style/AppTheme.Button
假的
真的
@颜色/原色暗

简短回答:试着去看看源代码是如何
Theme.MaterialComponents.Light.NoActionBar
正在定义 单击按钮并覆盖这些属性

Android样式通过合并主题中所有已定义的属性来工作。例如,您有一个
AppTheme
,其中包括每个内置视图的主题。如果您为按钮创建一个新主题,并且仅定义两个属性,Android studio所做的是合并
AppTheme
中定义的所有属性因此,如果要覆盖按钮,需要深入了解在应用程序中设置的常规
AppTheme
中定义的属性,它们可能不相同

例如,这就是我如何更改应用程序中所有按钮的颜色,因为它们使用全局属性: 在general styles.xml文件中,我将:

<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
    ...
    <item name="colorButtonNormal">?myCustomColor</item>
    ...
</style>
另外,请查看本主题中的其他开发人员体验

简短回答:试着去看看源代码是如何
Theme.MaterialComponents.Light.NoActionBar
正在定义 单击按钮并覆盖这些属性

Android样式通过合并主题中所有已定义的属性来工作。例如,您有一个
AppTheme
,其中包括每个内置视图的主题。如果您为按钮创建一个新主题,并且仅定义两个属性,Android studio所做的是合并
AppTheme
中定义的所有属性因此,如果要覆盖按钮,需要深入了解在应用程序中设置的常规
AppTheme
中定义的属性,它们可能不相同

例如,这就是我如何更改应用程序中所有按钮的颜色,因为它们使用全局属性: 在general styles.xml文件中,我将:

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

另外,请查看本主题中的另一个开发人员体验。

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

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

可以在材料设计文档中找到可替代材料组件的所有相关特性:

最后的代码如下所示:

<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>

@颜色/原色
@颜色/原色暗
@颜色/颜色重音
@颜色/颜色重音
@style/AppTheme.Test
@style/AppTheme.Button
#D400EB
#A305B1
假的
真的

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

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

可以在材料设计文档中找到可替代材料组件的所有相关特性:

最后的代码如下所示:

<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>

@颜色/原色
@颜色/原色暗
@颜色/颜色重音
@颜色/颜色重音
@style/AppTheme.Test
@style/AppTheme.Button
#D400EB
#A305B1
假的
真的

看起来MaterialComponents有问题。当使用AppCompact时,上面的操作很好。哦,我刚刚看到了这条评论。因此,正如我所提到的,每个主题可能会定义它们设置颜色的方式,所以请尝试查看
主题.MaterialComponents.Light.NoActionBar如何定义按钮和ov的样式的源代码erride那些属性。看起来MaterialComponents有问题。当使用AppCompact时,上面的工作很好。哦,我刚刚看到了这条评论。所以,正如我所提到的,每个主题可能定义它们设置颜色的方式,所以试着转到
主题.MaterialComponents.Light.NoActionBar
如何定义t的样式的源代码他按下按钮并覆盖这些属性。谢谢你的回答。我实际上找到了我问题的解决方案,这基本上是你解释的,但与材料成分有关。很高兴你找到了你的解决方案。谢谢你的回答。我实际上找到了我问题的解决方案,这基本上就是你解释的但是关于材料成分,很高兴你找到了解决方案。