【问题标题】:Change highlighted text color更改突出显示的文本颜色
【发布时间】:2016-03-29 19:05:48
【问题描述】:

当用户选择要复制粘贴的文本时,如何更改突出显示文本的文本颜色。



在这张图片中我想改变文字的颜色

世界

从黑到白。我该怎么做?

我尝试将 ColorStateList 添加为可绘制对象,但没有帮助。 我的文本视图:

<TextView
    android:id="@+id/tv_hello"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Hello World!"
    android:textColorHighlight="@color/light_blue"
    android:textIsSelectable="true"/>

【问题讨论】:

    标签: android xml android-layout android-studio textview


    【解决方案1】:

    在您的 res 目录中创建颜色文件夹。然后添加这个 xml 文件。让我们 text_color_change.xml

    res/color/text_color_change.xml

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

    然后在TextView中,将textColor添加为上述文件。

    <TextView
        ....
        android:textColor="@color/text_color_change" />
    

    【讨论】:

    • 这会为选定的文本提供背景信息。我已经添加了这个。我想更改突出显示文本的textColor
    • 我已经尝试过了,但不幸的是这也不起作用。
    【解决方案2】:

    添加此属性选择器属性,所选项目将保持其颜色状态,直到选择其他内容。 在你的

    selector.xml添加这个

            <!-- Activated -->      
    <item 
        android:state_activated="true" 
        android:color="#ff0000" />
    
            <!-- Active -->      
    <item 
        android:state_active="true" 
        android:color="#ff0000" />  
    

    查看this了解更多详情。

    【讨论】:

    • 我已经尝试过了,但不幸的是这也不起作用。
    • 你是对的,它会在选择时改变颜色。但问题是它改变了整个 textView textColor,因为我只想改变选定的 textColor。
    【解决方案3】:

    在您的 res/color 文件夹中创建一个名为 text_color_selector.xml

    的文件
    <?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
    
        <item android:state_pressed="true" android:color="#d48383"/>
        <item android:state_pressed="false" android:color="#121212"/>
        <item android:state_selected="true" android:color="#d48383"/>
        <item android:state_focused="true" android:color="#d48383"/>
    
    </selector>
    

    然后在您的 TextView 中将其设置为:

    android:textColor="@color/text_color_selector"
    

    【讨论】:

    • 我已经尝试过了,但不幸的是这也不起作用。
    【解决方案4】:

    如果不自己动手,我不确定是否可行:

    public class MyTextView extends TextView {
    
        ... Constructors, ...
    
        private ForegroundColorSpan mSpan = new ForegroundColorSpan(0xffff0000);
    
        @Override
        public void setText(CharSequence text, BufferType type) {
            // make sure the text is spannable
            if (type == BufferType.NORMAL) {
                type = BufferType.SPANNABLE;
            }
            super.setText(text, type);
        }
    
        @Override
        protected void onSelectionChanged(int selStart, int selEnd) {
            super.onSelectionChanged(selStart, selEnd);
    
            Spannable txt = (Spannable) getText();
    
            // ok even if not currently attached
            txt.removeSpan(mSpan);
    
            if (selStart != selEnd) {
                txt.setSpan(mSpan, selStart, selEnd, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-07-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-04-22
      • 2010-12-12
      • 2010-09-29
      相关资源
      最近更新 更多