【问题标题】:Android : change button text and background colorAndroid:更改按钮文本和背景颜色
【发布时间】:2013-10-24 09:48:10
【问题描述】:

如何在按下按钮时使用 xml 更改文本和背景颜色?

我可以改变文字颜色:

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

我可以改变背景(在带有可绘制参考的选择器/项目中使用它):

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

但是我怎样才能做到这两点呢?假设我想要:

  • 默认:黑色文本/白色背景
  • 按下:白色文本/蓝色背景

编辑:回答

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

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

在 mybackgroundcolors.xml 我管理背景,在 filtersbuttoncolors.xml 我管理文本颜色。在两个 xml 文件中,我管理状态(按下、选中、默认)

【问题讨论】:

标签: android button colors background status


【解决方案1】:

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

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

【讨论】:

  • 好的,但是什么会改变文本颜色?我必须使用 9patch 作为背景吗?
  • 您可以通过编程方式更改文本颜色.. 或通过 style.xml
  • 天哪,你是对的……我没想到。谢谢我更新我的话题给出答案
  • 当然做到了,我也在我的问题中添加了解释
【解决方案2】:

当您创建应用程序时,将在您的 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" 是 Google 的原生颜色。以下是原生样式的参考: https://android.googlesource.com/platform/frameworks/base/+/refs/heads/master/core/res/res/values/themes.xml

name="Theme.AppBaseTheme" 表示您正在创建一个继承parent="@android:style/Theme.Light" 的所有样式的样式。 这部分你可以忽略,除非你想再次从 AppBaseTheme 继承。 = &lt;style name="AppTheme" parent="AppBaseTheme"&gt;

@drawable/custom_background 是我放在 drawable 文件夹中的自定义图像。这是一张 300x300 的 png 图片。

#295055 是深蓝色。

我的代码更改了背景和文本颜色。对于 Button 文本,请查看 Google 的原生 stlyes(我在上面给你的链接)。

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

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

【讨论】:

    【解决方案3】:

    我认为这样做更简单:

    button.setBackgroundColor(Color.BLACK);
    

    你需要import android.graphics.Color;而不是:import android.R.color;

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

    【讨论】:

    • 这会在您选择按钮时失去按钮的颜色,并且还会失去主题在按钮上暗示的任何阴影或形状。
    • 它也是硬编码一个值,应该是主题。
    【解决方案4】:

    从 API 级别 21 开始,您可以使用:

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

    你只需要在你的xml中添加这个

    【讨论】:

    • 这是一个更好的解决方案,我不知道为什么 android 仅仅更改按钮背景颜色就变得如此复杂。并保持默认行为。
    • @Bhaskara 你可以使用支持库app:backgroundTint="@android:color/white"
    • 我浏览了很多方法,如何在 XML 中覆盖主题中指定的默认按钮背景颜色,只有这个有效。
    【解决方案5】:

    在styles.xml中添加下一行

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

    在按钮中,添加android:theme="@style/AppTheme.Gray",例如:

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

    【讨论】:

    • 我尝试了同样的方法,但它不起作用。
    【解决方案6】:

    只是补充@Jonsmoke 的回答。

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

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

    在 XML 中用于按钮布局。

    对于低于 21 的 API 级别,请使用 AppCompatButton 使用 app 命名空间,而不是 backgroundTintandroid。 p>

    例如:

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

    【讨论】:

      【解决方案7】:

      只需使用 MaterialButtonapp:backgroundTintandroid:textColor 属性:

      <MaterialButton
        app:backgroundTint="@color/my_color"
        android:textColor="@android:color/white"/>
      

      【讨论】:

        猜你喜欢
        • 2020-11-07
        • 2021-07-05
        • 1970-01-01
        • 2015-06-04
        • 1970-01-01
        • 1970-01-01
        • 2017-08-31
        • 1970-01-01
        相关资源
        最近更新 更多