更改背景颜色时TextView文本颜色会发生变化

时间:2022-11-04 21:17:08

I am using TextView as a Button (flat UI) in my android application. Below is the code

我在我的Android应用程序中使用TextView作为Button(平面UI)。以下是代码

<TextView
            android:id="@+id/button"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="8dp"
            android:layout_marginRight="8dp"
            android:background="@drawable/button_background"
            android:enabled="false"
            android:gravity="center"
            android:paddingBottom="16dp"
            android:paddingTop="16dp"
            android:text="Sign Up"
            android:textColor="@color/white"
            android:textSize="16sp"
            android:textStyle="bold" />

The background drawable 'button_background' is

背景drawable'button_background'是

<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#FCD5A5" android:state_enabled="false" />
<item android:drawable="#F7941E" />

So when Button is enabled it should have dark orange background otherwise light orange background.

因此,当启用Button时,它应该具有深橙色背景,否则为浅橙色背景。

Background color is working fine with both the states (enabled and disabled) but text color is also getting changed. It remains white in enabled state but changes to dark grey in disabled state. I want to keep it white in both the states.

背景颜色在两种状态(启用和禁用)下都能正常工作,但文本颜色也会发生变化。它在启用状态下保持白色,但在禁用状态下更改为深灰色。我想在这两个州保持白色。

2 个解决方案

#1


0  

I'm currently looking at how to do this for your selector. But for now, you could always do this and call it once to initialize and then when the state changes:

我目前正在考虑如何为您的选择器执行此操作。但是现在,您可以始终执行此操作并将其调用一次以进行初始化,然后在状态更改时调用:

private void updateTextColor(TextView view, Context context) {
    if (!view.isEnabled()) {
        view.setTextColor(context.getResources().getColor(android.R.color.white));
    }
}

#2


0  

textcolorselector.xml <= put this file in drawable

textcolorselector.xml <=将此文件放在drawable中

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

<item android:color="@color/general_blue" android:state_enabled="true"></item>
<item android:color="@color/general_gray" android:state_enabled="false"></item>
<item android:color="@color/general_blue"></item>

add this line to color.xml file

将此行添加到color.xml文件中

 <drawable name="textviewcolor">@drawable/textcolorselector</drawable>

and finally apply this to your layout

最后将其应用于您的布局

<TextView
        android:id="@+id/button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="8dp"
        android:layout_marginRight="8dp"
        android:background="@drawable/button_background"
        android:enabled="false"
        android:gravity="center"
        android:paddingBottom="16dp"
        android:paddingTop="16dp"
        android:text="Sign Up"
        android:textColor="@color/textviewcolor" // <== i made change here!
        android:textSize="16sp"
        android:textStyle="bold" />

#1


0  

I'm currently looking at how to do this for your selector. But for now, you could always do this and call it once to initialize and then when the state changes:

我目前正在考虑如何为您的选择器执行此操作。但是现在,您可以始终执行此操作并将其调用一次以进行初始化,然后在状态更改时调用:

private void updateTextColor(TextView view, Context context) {
    if (!view.isEnabled()) {
        view.setTextColor(context.getResources().getColor(android.R.color.white));
    }
}

#2


0  

textcolorselector.xml <= put this file in drawable

textcolorselector.xml <=将此文件放在drawable中

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

<item android:color="@color/general_blue" android:state_enabled="true"></item>
<item android:color="@color/general_gray" android:state_enabled="false"></item>
<item android:color="@color/general_blue"></item>

add this line to color.xml file

将此行添加到color.xml文件中

 <drawable name="textviewcolor">@drawable/textcolorselector</drawable>

and finally apply this to your layout

最后将其应用于您的布局

<TextView
        android:id="@+id/button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="8dp"
        android:layout_marginRight="8dp"
        android:background="@drawable/button_background"
        android:enabled="false"
        android:gravity="center"
        android:paddingBottom="16dp"
        android:paddingTop="16dp"
        android:text="Sign Up"
        android:textColor="@color/textviewcolor" // <== i made change here!
        android:textSize="16sp"
        android:textStyle="bold" />