简体   繁体   中英

TextView text color changes on changing the background color

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

<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

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

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.

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));
    }
}

textcolorselector.xml <= put this file in 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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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