change statelistdrawable text color android button

为君一笑 提交于 2019-12-12 12:27:36

问题


I'm developing android application

I have a different background drawable and text color for each status of the button (pressed , normal)

I have created statelistdrawable object to be able to add the background drawable , but my problem now is how to set the text color

can any one help please ?


回答1:


Button is a TextView and you can call button.setTextColor(someColorStateList) on a TextView.

Here is how to do it programmatically:

   ColorStateList colorStateList = new ColorStateList(
            new int[][]{
                    new int[]{R.attr.state_pressed},
                    new int[]{R.attr.state_selected},
                    new int[]{-R.attr.state_selected},
            },
            new int[]{
                    Color.GREEN,
                    Color.BLUE,
                    Color.RED});
    TextView textView = ... some textView or button
    textView.setTextColor(colorStateList);

You can of course do the same with xml configuration (define your colorStateList in xml and associate it with the button text color property android:textColor="@drawable/mycolorstatelist"



来源:https://stackoverflow.com/questions/14604385/change-statelistdrawable-text-color-android-button

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!