安卓默认按钮颜色

11
当我打开一个新的Android Studio项目时,默认按钮颜色为紫色。我希望默认颜色为灰色的默认按钮颜色(我想你知道我的意思)。我尝试通过XML和Java更改颜色,但都没有成功。我希望默认按钮颜色为灰色,而不需要每次都改变它。 enter image description here enter image description here themes.xml:
<resources xmlns:tools="http://schemas.android.com/tools">
<!-- Base application theme. -->
<style name="Theme.HangmanGame" parent="Theme.MaterialComponents.DayNight.DarkActionBar">
    <!-- Primary brand color. -->
    <item name="colorPrimary">@color/purple_500</item>
    <item name="colorPrimaryVariant">@color/purple_700</item>
    <item name="colorOnPrimary">@color/white</item>
    <!-- Secondary brand color. -->
    <item name="colorSecondary">@color/teal_200</item>
    <item name="colorSecondaryVariant">@color/teal_700</item>
    <item name="colorOnSecondary">@color/black</item>
    <!-- Status bar color. -->
    <item name="android:statusBarColor" tools:targetApi="l">?attr/colorPrimaryVariant</item>
    <!-- Customize your theme here. -->
</style>

themes.xml(night)

<resources xmlns:tools="http://schemas.android.com/tools">
<!-- Base application theme. -->
<style name="Theme.HangmanGame" parent="Theme.MaterialComponents.DayNight.DarkActionBar">
    <!-- Primary brand color. -->
    <item name="colorPrimary">@color/purple_200</item>
    <item name="colorPrimaryVariant">@color/purple_700</item>
    <item name="colorOnPrimary">@color/black</item>
    <!-- Secondary brand color. -->
    <item name="colorSecondary">@color/teal_200</item>
    <item name="colorSecondaryVariant">@color/teal_200</item>
    <item name="colorOnSecondary">@color/black</item>
    <!-- Status bar color. -->
    <item name="android:statusBarColor" tools:targetApi="l">?attr/colorPrimaryVariant</item>
    <!-- Customize your theme here. -->
</style>

你是否正在使用Material组件库? - Gabriele Mariotti
不,按钮默认是紫色。 - Tomer
发布您的应用主题。 - Gabriele Mariotti
你的colorPrimary和colorPrimaryVariant被设置为紫色。将它们改为灰色不起作用吗? - Md. Tahmid Mozaffar
默认按钮颜色的十六进制值是多少? - Tomer
@Tomer 你正在使用 MaterialComponents 主题。请查看下面的答案。按钮的背景颜色基于 <item name="colorPrimary">@color/purple_500</item> - Gabriele Mariotti
3个回答

13

如果您使用 Theme.MaterialComponents.* 主题,则Button 的默认背景颜色(被 MaterialButton 替换)是在应用程序主题中定义的colorPrimary
在您的情况下:

<item name="colorPrimary">@color/purple_500</item>

你可以更改此值(但这将影响所有小部件)。

如果您想全局更改应用程序中的按钮样式,还可以在应用程序主题中添加materialButtonStyle属性:

<style name="Theme.HangmanGame" parent="Theme.MaterialComponents.DayNight.DarkActionBar">
   <item name="materialButtonStyle">@style/Widget.App.Button</item>
</style>

使用:

<style name="Widget.App.Button" parent="Widget.MaterialComponents.Button">
    <item name="backgroundTint">@color/...</item>
</style>

如果您只想更改按钮的颜色,也可以使用app:backgroundTint属性来替换android:background属性:
<Button
    app:backgroundTint="@color/..."/>

如果您想使用自定义背景,可以使用 android:background 属性,但是为了避免按钮被染色,您需要添加 app:backgroundTint="@null".


它可以工作的 material3 api 31。 - MindRoasterMir

2
我认为最简单的方法是进入三个xml文件:src\main\res\values\colors.xml、src\main\res\values\themes.xml和src\main\res\values-night\themes.xml。
在colors.xml中,添加一个名为“button”的颜色,并将颜色设置为所需的颜色。有关一份简单的颜色代码列表,请参见此答案:https://dev59.com/sG865IYBdhLWcg3whfBg#7323234 该行应该类似于这样:
<color name="button">#808080</color>  //That code is the gray you want.

在两个主题文件中,将colorPrimary更改为"@color/button"。这将影响应用程序中的每个按钮。

0

你可以从themes.xml中更改应用程序主题:

<style name="Theme.DemoApp" parent="Theme.MaterialComponents.DayNight.DarkActionBar">

转换为:

    <style name="Theme.DemoApp" parent="Theme.AppCompat.DayNight.DarkActionBar">

接着,添加按钮背景:

<Button
        android:id="@+id/button"
        android:layout_width="120dp"
        android:layout_height="wrap_content"
        **android:background="@color/white"**
        android:text="Submit"/>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接