【问题标题】:Changing Textcolor and Background of a TextView更改 TextView 的 Textcolor 和背景
【发布时间】:2012-04-19 08:06:54
【问题描述】:

我是 Android 开发的新手。

如果 ListViewItem 被选中或按下,我会尝试更改其 FontColor 和背景。 使用以下选择器更改背景绝对没有问题:

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

但是如何更改背景和字体颜色?

【问题讨论】:

    标签: android colors fonts background selector


    【解决方案1】:

    更改 TextView 的字体颜色的方法与列表项的背景颜色相同:创建一个 StateListDrawable 并将 TextViewandroid:textColor 设置为您的自定义可绘制对象。

    例如,请参阅Change ListViews text color on click。将此与您已有的列表项背景相结合。

    如果您希望颜色相互淡入,请在&lt;selector&gt;-tag 中设置(例如)android:exitFadeDuration="@android:integer/config_mediumAnimTime"

    【讨论】:

    • 谢谢,就是这样。也感谢您对褪色的特别说明。
    【解决方案2】:

    试试这个

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

    【讨论】:

    • Minh Binh 你能告诉我如何在 textview 的情况下更改悬停时的 textcolor 而我的 textview 在行内
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-26
    • 2014-06-12
    • 2011-06-05
    • 1970-01-01
    相关资源
    最近更新 更多