Changing Textcolor and Background of a TextView

荒凉一梦 提交于 2019-12-08 06:07:27

问题


I am totaly new in Android development.

I try to change the FontColor and the Background of ListViewItem if it`s selected or pressed. Changing the Background is absolutely no Problem by using following selector:

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

   <item android:state_pressed="true"
         android:drawable="@drawable/background_pressed"></item>

   <item android:state_focused="true"
         android:drawable="@drawable/background_pressed"></item>

   <item android:state_selected="true"
         android:drawable="@drawable/background_pressed"></item>

   <item android:drawable="@android:color/transparent"></item>

</selector>

But how can I change the Background and the FontColor?


回答1:


Changing the TextView's font color is done in the same way as the listitem's backgroundcolor: create a StateListDrawable and set the TextView's android:textColor to your custom drawable.

For an example, see Change ListViews text color on click. Combine this with what you already have for the listitem's background.

If you want the colors to fade into eachother, set (e.g.) android:exitFadeDuration="@android:integer/config_mediumAnimTime" in the <selector>-tag.




回答2:


Try this

    @Override
public void onCreate(Bundle savedInstanceState) {
    TextView txtName;
    txtName.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            TextView textView = (TextView) v;
            textView.setBackgroundColor(Color.RED);
            textView.setTextColor(Color.YELLOW);
        }
    });
}


来源:https://stackoverflow.com/questions/10031060/changing-textcolor-and-background-of-a-textview

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