Android:改变按钮文本和背景颜色

时间:2022-04-05 09:39:34

How can I change both text and background colors when my button is pressed, with xml ?

当按下按钮时,如何使用xml修改文本和背景颜色?

To change text color I can do :

改变文字颜色我可以做:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true" android:color="mycolor"/>
    <item android:color="mycolor2/>
</selector>

To change the background I can do (using it in a selector/item with drawable reference) :

要更改背景,我可以这样做(在选择器/项目中使用它并提供可绘制引用):

<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <solid android:color="#FF0079FF" />
</shape>

But how can I do both ? Let's say I want to have :

但是我怎么能同时做到这两点呢?假设我想要:

  • Default : black text / white background
  • 默认:黑色文本/白色背景
  • Pressed : white text / blue background
  • 按下:白色文字/蓝色背景

EDIT : answer

I totaly forgot that the background and text color are managed separately, so this is how I did it :

我完全忘记了背景和文本颜色是分开管理的,所以我这样做:

<Button
    android:textColor="@color/filtersbuttoncolors"
    android:background="@drawable/mybackgroundcolors" />

In mybackgroundcolors.xml I manage the background and in filtersbuttoncolors.xml I manage the text color. In both xml files I manage the status (pressed, selected, default)

在mybackgroundcolors。我管理后台和filtersbuttoncolors。我管理文本颜色。在这两个xml文件中,我都管理状态(按下、选择、默认)

6 个解决方案

#1


36  

Here is an example of a drawable that will be white by default, black when pressed:

这里有一个可绘制的示例,默认为白色,按下时为黑色:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true">
        <shape>
            <solid
                android:color="#1E669B"/>
            <stroke
                android:width="2dp"
                android:color="#1B5E91"/>
            <corners
                android:radius="6dp"/>
            <padding
                android:bottom="10dp"
                android:left="10dp"
                android:right="10dp"
                android:top="10dp"/>
        </shape>

    </item>
    <item>
        <shape>
            <gradient
                android:angle="270"
                android:endColor="#1E669B"
                android:startColor="#1E669B"/>
            <stroke
                android:width="4dp"
                android:color="#1B5E91"/>
            <corners
                android:radius="7dp"/>
            <padding
                android:bottom="10dp"
                android:left="10dp"
                android:right="10dp"
                android:top="10dp"/>
        </shape>
    </item>
</selector>

#2


19  

I think doing this way is much simpler:

我认为这样做要简单得多:

button.setBackgroundColor(Color.BLACK);

And you need to import android.graphics.Color; not: import android.R.color;

你需要导入android。graphics。color;不是:进口android.R.color;

Or you can just write the 4-byte hex code (not 3-byte) 0xFF000000 where the first byte is setting the alpha.

或者您可以只编写4字节的十六进制代码(不是3字节)0xFF000000,其中第一个字节正在设置alpha。

#3


14  

Since API level 21 you can use :

由于API级别为21,您可以使用:

android:backgroundTint="@android:color/white"

you only have to add this in your xml

您只需在xml中添加它

#4


7  

add below line in styles.xml

在styles.xml中添加以下行

<style name="AppTheme.Gray" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
        <item name="colorButtonNormal">@color/colorGray</item>
    </style>

in button, add android:theme="@style/AppTheme.Gray", example:

在按钮,添加android:主题= " @style / AppTheme。灰色”,例如:

<Button
            android:theme="@style/AppTheme.Gray"
            android:textColor="@color/colorWhite"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@android:string/cancel"/>

#5


3  

When you create an App, a file called styles.xml will be created in your res/values folder. If you change the styles, you can change the background, text color, etc for all your layouts. That way you don’t have to go into each individual layout and change the it manually.

当你创建一个应用程序时,一个叫做styles的文件。xml将在res/values文件夹中创建。如果你改变风格,你可以改变背景,文本颜色,等等所有你的布局。这样,您不必进入每个单独的布局并手动更改它。

styles.xml:

styles.xml:

<resources xmlns:android="http://schemas.android.com/apk/res/android">
<style name="Theme.AppBaseTheme" parent="@android:style/Theme.Light">
    <item name="android:editTextColor">#295055</item> 
    <item name="android:textColorPrimary">#295055</item>
    <item name="android:textColorSecondary">#295055</item>
    <item name="android:textColorTertiary">#295055</item>
    <item name="android:textColorPrimaryInverse">#295055</item>
    <item name="android:textColorSecondaryInverse">#295055</item>
    <item name="android:textColorTertiaryInverse">#295055</item>

     <item name="android:windowBackground">@drawable/custom_background</item>        
</style>

<!-- Application theme. -->
<style name="AppTheme" parent="AppBaseTheme">
    <!-- All customizations that are NOT specific to a particular API-level can go here. -->
</style>

parent="@android:style/Theme.Light" is Google’s native colors. Here is a reference of what the native styles are: https://android.googlesource.com/platform/frameworks/base/+/refs/heads/master/core/res/res/values/themes.xml

父母= " @android:风格/主题。Light是谷歌的原生颜色。这里有一个关于本机风格的参考:https://android.googlesource.com/platform/frameworks/base// refs/head /master/core/res/ values/themes.xml。

name="Theme.AppBaseTheme" means that you are creating a style that inherits all the styles from parent="@android:style/Theme.Light". This part you can ignore unless you want to inherit from AppBaseTheme again. = <style name="AppTheme" parent="AppBaseTheme">

name = "的主题。AppBaseTheme“意味着您正在创建一种样式,该样式继承父="@android:style/Theme.Light"的所有样式。如果不想从AppBaseTheme继承,可以忽略这一部分。= <样式名= " apptheme“父=" AppBaseTheme ">

@drawable/custom_background is a custom image I put in the drawable’s folder. It is a 300x300 png image.

@drawable/custom_background是我放在drawable文件夹中的一个自定义映像。这是一个300x300png图像。

#295055 is a dark blue color.

295055是深蓝色。

My code changes the background and text color. For Button text, please look through Google’s native stlyes (the link I gave u above).

我的代码改变了背景和文本的颜色。对于按钮文本,请查看谷歌的本地stlyes(我在上面给出的链接)。

Then in Android Manifest, remember to include the code:

然后在Android Manifest中,记得包含代码:

<application
android:theme="@style/Theme.AppBaseTheme">

#6


0  

Just complementing @Jonsmoke's answer.

只是补充@Jonsmoke回答。

For API level 21 and above you can use :

对于API级别21及以上,你可使用:

android:backgroundTint="@android:color/white"

in XML for the button layout.

在XML中用于按钮布局。

For API level below 21 use an AppCompatButton using app namespace instead of android for backgroundTint.

对于API级别低于21的用户,使用AppCompatButton代替android使用app命名空间作为背景色。

For example:

例如:

<android.support.v7.widget.AppCompatButton
    android:id="@+id/my_button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="My Button"
    app:backgroundTint="@android:color/white" />

#1


36  

Here is an example of a drawable that will be white by default, black when pressed:

这里有一个可绘制的示例,默认为白色,按下时为黑色:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true">
        <shape>
            <solid
                android:color="#1E669B"/>
            <stroke
                android:width="2dp"
                android:color="#1B5E91"/>
            <corners
                android:radius="6dp"/>
            <padding
                android:bottom="10dp"
                android:left="10dp"
                android:right="10dp"
                android:top="10dp"/>
        </shape>

    </item>
    <item>
        <shape>
            <gradient
                android:angle="270"
                android:endColor="#1E669B"
                android:startColor="#1E669B"/>
            <stroke
                android:width="4dp"
                android:color="#1B5E91"/>
            <corners
                android:radius="7dp"/>
            <padding
                android:bottom="10dp"
                android:left="10dp"
                android:right="10dp"
                android:top="10dp"/>
        </shape>
    </item>
</selector>

#2


19  

I think doing this way is much simpler:

我认为这样做要简单得多:

button.setBackgroundColor(Color.BLACK);

And you need to import android.graphics.Color; not: import android.R.color;

你需要导入android。graphics。color;不是:进口android.R.color;

Or you can just write the 4-byte hex code (not 3-byte) 0xFF000000 where the first byte is setting the alpha.

或者您可以只编写4字节的十六进制代码(不是3字节)0xFF000000,其中第一个字节正在设置alpha。

#3


14  

Since API level 21 you can use :

由于API级别为21,您可以使用:

android:backgroundTint="@android:color/white"

you only have to add this in your xml

您只需在xml中添加它

#4


7  

add below line in styles.xml

在styles.xml中添加以下行

<style name="AppTheme.Gray" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
        <item name="colorButtonNormal">@color/colorGray</item>
    </style>

in button, add android:theme="@style/AppTheme.Gray", example:

在按钮,添加android:主题= " @style / AppTheme。灰色”,例如:

<Button
            android:theme="@style/AppTheme.Gray"
            android:textColor="@color/colorWhite"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@android:string/cancel"/>

#5


3  

When you create an App, a file called styles.xml will be created in your res/values folder. If you change the styles, you can change the background, text color, etc for all your layouts. That way you don’t have to go into each individual layout and change the it manually.

当你创建一个应用程序时,一个叫做styles的文件。xml将在res/values文件夹中创建。如果你改变风格,你可以改变背景,文本颜色,等等所有你的布局。这样,您不必进入每个单独的布局并手动更改它。

styles.xml:

styles.xml:

<resources xmlns:android="http://schemas.android.com/apk/res/android">
<style name="Theme.AppBaseTheme" parent="@android:style/Theme.Light">
    <item name="android:editTextColor">#295055</item> 
    <item name="android:textColorPrimary">#295055</item>
    <item name="android:textColorSecondary">#295055</item>
    <item name="android:textColorTertiary">#295055</item>
    <item name="android:textColorPrimaryInverse">#295055</item>
    <item name="android:textColorSecondaryInverse">#295055</item>
    <item name="android:textColorTertiaryInverse">#295055</item>

     <item name="android:windowBackground">@drawable/custom_background</item>        
</style>

<!-- Application theme. -->
<style name="AppTheme" parent="AppBaseTheme">
    <!-- All customizations that are NOT specific to a particular API-level can go here. -->
</style>

parent="@android:style/Theme.Light" is Google’s native colors. Here is a reference of what the native styles are: https://android.googlesource.com/platform/frameworks/base/+/refs/heads/master/core/res/res/values/themes.xml

父母= " @android:风格/主题。Light是谷歌的原生颜色。这里有一个关于本机风格的参考:https://android.googlesource.com/platform/frameworks/base// refs/head /master/core/res/ values/themes.xml。

name="Theme.AppBaseTheme" means that you are creating a style that inherits all the styles from parent="@android:style/Theme.Light". This part you can ignore unless you want to inherit from AppBaseTheme again. = <style name="AppTheme" parent="AppBaseTheme">

name = "的主题。AppBaseTheme“意味着您正在创建一种样式,该样式继承父="@android:style/Theme.Light"的所有样式。如果不想从AppBaseTheme继承,可以忽略这一部分。= <样式名= " apptheme“父=" AppBaseTheme ">

@drawable/custom_background is a custom image I put in the drawable’s folder. It is a 300x300 png image.

@drawable/custom_background是我放在drawable文件夹中的一个自定义映像。这是一个300x300png图像。

#295055 is a dark blue color.

295055是深蓝色。

My code changes the background and text color. For Button text, please look through Google’s native stlyes (the link I gave u above).

我的代码改变了背景和文本的颜色。对于按钮文本,请查看谷歌的本地stlyes(我在上面给出的链接)。

Then in Android Manifest, remember to include the code:

然后在Android Manifest中,记得包含代码:

<application
android:theme="@style/Theme.AppBaseTheme">

#6


0  

Just complementing @Jonsmoke's answer.

只是补充@Jonsmoke回答。

For API level 21 and above you can use :

对于API级别21及以上,你可使用:

android:backgroundTint="@android:color/white"

in XML for the button layout.

在XML中用于按钮布局。

For API level below 21 use an AppCompatButton using app namespace instead of android for backgroundTint.

对于API级别低于21的用户,使用AppCompatButton代替android使用app命名空间作为背景色。

For example:

例如:

<android.support.v7.widget.AppCompatButton
    android:id="@+id/my_button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="My Button"
    app:backgroundTint="@android:color/white" />