文本颜色选择器不起作用 [英] Textcolor selector not working

查看:25
本文介绍了文本颜色选择器不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我以前做过.我复制粘贴.我从网上复制粘贴了许多其他示例.我根本无法使 textcolor 选择器工作.它将默认颜色设置为 textview,但如果单击 textview,它不会改变.背景的 settings_selector 工作正常.

I did this before. I copy-pasted. I copy-pasted many other examples from the net. I simply cannot make the textcolor selector work. It sets the default color to the textview, but it won't change if you click on the textview. The settings_selector for the background works fine.

这是布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/settings_selector"
    android:clickable="true"
    android:id="@+id/llRecentChanges"
    android:paddingTop="5dp"
    android:paddingBottom="5dp"
    android:paddingLeft="5dp">
      <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/llRecentChanges2"
        android:layout_weight="1"
        android:layout_gravity="center_vertical">
    <TextView
        android:id="@+id/tvAbout"
        android:text="@string/settings_recentchanges"
        android:gravity="center_vertical"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="16dp"
        android:textColor="@drawable/settings_selector_txt" >
    </TextView>
    <TextView
        android:id="@+id/tvAbout2"
        android:text="@string/settings_recentchanges2"
        android:gravity="center_vertical"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="@drawable/settings_selector_txt"
        android:textSize="10dp">
    </TextView>
    </LinearLayout>

</LinearLayout>

这是 settings_selector_txt xml:

This is the settings_selector_txt xml:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_focused="true" 
          android:state_pressed="false" 
          android:color="#FFFFFF" />
    <item android:state_focused="true" 
          android:state_pressed="true"
          android:color="#ffa800" />
    <item android:state_focused="false" 
          android:state_pressed="true"
          android:color="#ffa800" />
    <item android:color="#FFFFFF" />
</selector>

或者这个

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

    <item android:state_selected="true" android:color="#444"/>
    <item android:state_focused="true" android:color="#444"/>
    <item android:state_pressed="true" android:color="#444"/>
    <item android:color="#ccc"/>

</selector>

或者这个

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_focused="true" android:state_pressed="false" android:color="#ffffff" />
    <item android:state_focused="true" android:state_pressed="true" android:color="#000000" />
    <item android:state_focused="false" android:state_pressed="true" android:color="#000000" />
    <item android:color="#ffffff" />
</selector>

它们都不起作用.将选择器 xml 放入颜色文件夹也不是解决方案.有什么想法吗?

None of them is working. Putting the selector xml to the color folder is also no solution. Any ideas?

推荐答案

确保您的 TextView 已准备好监听您申请的州.例如,为了能够到达state_pressed",您的 textView 应该是可点击的:

Make sure your TextView is ready for listening the states you are applying for. For instance, to be able to reach the "state_pressed" your textView should be clickable:

android:clickable="true"

我们开始了.这种布局可以完成这项工作.请注意,收集点击事件的 View 是 linearLayout,但 TextView 会重现它,因为duplicateParentState"设置为 true.颜色选择器会处理不同状态的颜色.

There we go. This layout does the job. Note that the View that gathers the click event is the linearLayout, but the TextView reproduces it because of "duplicateParentState" set to true. The color selector would take care of the colors for the different states.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/testLlayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:clickable="true" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="@drawable/textview_selector"
        android:duplicateParentState="true"
        android:text="TextView" />

</LinearLayout>

这是颜色选择器的代码.

And here is the code for the color selector.

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

    <item android:state_selected="true" android:color="#444"/>
    <item android:state_focused="true"  android:color="#444"/>
    <item android:state_pressed="true"  android:color="#444"/>
    <item android:color="#ccc"/>

</selector>

应该是这样的.

这篇关于文本颜色选择器不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆