简体   繁体   English

Android 默认按钮颜色

[英]Android default button color

When I open a new android studio project, the default color for button is purple.当我打开一个新的 android 工作室项目时,按钮的默认颜色是紫色。 I want the default color to be the gray default button color(I assume you know what I mean).我希望默认颜色是灰色默认按钮颜色(我假设你知道我的意思)。 I tried to change the color via xml and java and nothing worked.我试图通过 xml 和 java 更改颜色,但没有任何效果。 I want that the default button color will be gray without I'd have to change it every time.我希望默认按钮颜色为灰色,而不必每次都更改它。 在此处输入图像描述

在此处输入图像描述

themse.xml:他们.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)主题.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_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>

Since you are using a Theme.MaterialComponents.* theme the default background color of the Button (which is replaced by a MaterialButton ) is the colorPrimary defined in your app theme.由于您使用的是Theme.MaterialComponents.*主题, Button的默认背景颜色(由MaterialButton替换)是您的应用程序主题中定义的colorPrimary
In your case:在你的情况下:

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

You can change this value (but this will affect all widgets).您可以更改此值(但这会影响所有小部件)。

If you want to change globally the button style in your app you can also add the materialButtonStyle attribute in your app theme:如果要全局更改应用程序中的按钮样式,还可以在应用程序主题中添加materialButtonStyle属性:

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

with:和:

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

If you want to change this color only in the button you can use also the app:backgroundTint attribute removing the android:background attribute:如果您只想在按钮中更改此颜色,您还可以使用app:backgroundTint属性删除android:background属性:

<Button
    app:backgroundTint="@color/..."/>

If you want to use a custom background using the android:background attribute you have to add app:backgroundTint="@null" to avoid that the button is tinted.如果您想使用android:background属性使用自定义背景,您必须添加app:backgroundTint="@null"以避免按钮被着色。

I think the easiest way is to go into three xml files: src\\main\\res\\values\\colors.xml, src\\main\\res\\values\\themes.xml, and src\\main\\res\\values-night\\themes.xml我认为最简单的方法是进入三个 xml 文件:src\\main\\res\\values\\colors.xml、src\\main\\res\\values\\themes.xml 和 src\\main\\res\\values-night\\themes。 xml

In the colors.xml, add a color and call it "button", then set the color to what you want.在colors.xml 中,添加一种颜色并将其称为“按钮”,然后将颜色设置为您想要的颜色。 For a simple list of color codes, see this answer: https://stackoverflow.com/a/7323234/5374362有关颜色代码的简单列表,请参阅此答案: https : //stackoverflow.com/a/7323234/5374362

The line would look something like this:该行看起来像这样:

<color name="button">#808080</color>  //That code is the gray you want.

In the two themes files, change colorPrimary to "@color/button"在两个主题文件中,将 colorPrimary 更改为“@color/button”

This will affect every button in the app.这将影响应用程序中的每个按钮。

Either, you can change the application theme in themes.xml from:或者,您可以更改主题中的应用程序主题。xml 从:

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

to this:对此:

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

Then, add Button Background:然后,添加按钮背景:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM