Android默认按钮颜色

huangapple go评论68阅读模式
英文:

Android default button color

问题

themse.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/gray_button_color</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>
</resources>

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>
</resources>
英文:

When I open a new android studio project, the default color for button is purple. 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. I want that the default button color will be gray without I'd have to change it every time.
Android默认按钮颜色

Android默认按钮颜色

themse.xml:

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

</resources>

themes.xml(night)

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

</resources>

答案1

得分: 13

由于您正在使用**Theme.MaterialComponents.*主题,Button(由MaterialButton替代)的默认背景颜色是在您的应用主题中定义的colorPrimary**。在您的情况下:

&lt;item name=&quot;colorPrimary&quot;&gt;@color/purple_500&lt;/item&gt;

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

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

&lt;style name=&quot;Theme.HangmanGame&quot; parent=&quot;Theme.MaterialComponents.DayNight.DarkActionBar&quot;&gt;
   &lt;item name=&quot;materialButtonStyle&quot;&gt;@style/Widget.App.Button&lt;/item&gt;
&lt;/style&gt;

其中:

&lt;style name=&quot;Widget.App.Button&quot; parent=&quot;Widget.MaterialComponents.Button&quot;&gt;
    &lt;item name=&quot;backgroundTint&quot;&gt;@color/...&lt;/item&gt;
&lt;/style&gt;

如果您只想在按钮中更改此颜色,还可以使用app:backgroundTint属性,并删除android:background属性:

&lt;Button
    app:backgroundTint=&quot;@color/...&quot;/&gt;

如果您想要使用自定义背景,可以使用android:background属性,并添加app:backgroundTint=&quot;@null&quot;以避免对按钮进行着色。

英文:

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.
In your case:

&lt;item name=&quot;colorPrimary&quot;&gt;@color/purple_500&lt;/item&gt;

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:

&lt;style name=&quot;Theme.HangmanGame&quot; parent=&quot;Theme.MaterialComponents.DayNight.DarkActionBar&quot;&gt;
   &lt;item name=&quot;materialButtonStyle&quot;&gt;@style/Widget.App.Button&lt;/item&gt;
&lt;/style&gt;

with:

&lt;style name=&quot;Widget.App.Button&quot; parent=&quot;Widget.MaterialComponents.Button&quot;&gt;
    &lt;item name=&quot;backgroundTint&quot;&gt;@color/...&lt;/item&gt;
&lt;/style&gt;

If you want to change this color only in the button you can use also the app:backgroundTint attribute removing the android:background attribute:

&lt;Button
    app:backgroundTint=&quot;@color/...&quot;/&gt;

If you want to use a custom background using the android:background attribute you have to add app:backgroundTint=&quot;@null&quot; to avoid that the button is tinted.

答案2

得分: 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://stackoverflow.com/a/7323234/5374362

这一行看起来会类似于这样:

<color name="button">#808080</color>  //那段代码是你想要的灰色。

在两个主题文件中,将 colorPrimary 改为 "@color/button";

这将影响应用中的每个按钮。

英文:

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

In the colors.xml, add a color and call it "button", then set the color to what you want. For a simple list of color codes, see this answer: https://stackoverflow.com/a/7323234/5374362

The line would look something like this:

&lt;color name=&quot;button&quot;&gt;#808080&lt;/color&gt;  //That code is the gray you want.

In the two themes files, change colorPrimary to "@color/button"

This will affect every button in the app.

答案3

得分: 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"/>
英文:

Either, you can change the application theme in themes.xml from:

&lt;style name=&quot;Theme.DemoApp&quot; parent=&quot;Theme.MaterialComponents.DayNight.DarkActionBar&quot;&gt;

to this:

    &lt;style name=&quot;Theme.DemoApp&quot; parent=&quot;Theme.AppCompat.DayNight.DarkActionBar&quot;&gt;

Then, add Button Background:

&lt;Button
        android:id=&quot;@+id/button&quot;
        android:layout_width=&quot;120dp&quot;
        android:layout_height=&quot;wrap_content&quot;
        **android:background=&quot;@color/white&quot;**
        android:text=&quot;Submit&quot;/&gt;

huangapple
  • 本文由 发表于 2020年10月17日 16:22:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/64400433.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定