How to change textColor of a button from XML in Android?
I am using a selector
to change the background drawable of the button in different states (focused, pressed, normal). Is there any way to change the text color too? I want to provide variou开发者_如何转开发s text colors for various button states, but I want to do it from the xml. Is this possible?
Yes it can be done. You just do the same way you do for the button drawable. Then assign it to android:textColor="@drawable/yourselector"
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"
android:color="#000000" /> <!-- pressed -->
<item android:state_focused="true"
android:color="#000000" /> <!-- focused -->
<item android:color="#FFFFFF" /> <!-- default -->
</selector>
Try combining the above with the android:drawable
attribute.
If it's programmatically, you could use:
Button button = findViewById(R.id.yourbutton);
button.setTextColor(Color.yourcolor);
In xml it'll be the same thing.
Use android:color="#ff0000"
in selector for focused
and another color for default
state.Then in xml of button android:textColor="@drawable/yourselector"
精彩评论