简体   繁体   English

如何使Android TextView TextColor保持突出显示?

[英]How to make an Android TextView TextColor stay highlighted?

I have a TextView in a ListView.I want the TextColor of the TextView stay highlighted when selected.It's working fine in newer versions, but in lower versions it's not being highlighted, it's just blinking. 我在ListView中有一个TextView。我希望选中时TextView的TextColor保持突出显示。在新版本中可以正常工作,但在较低版本中则不突出显示,只是闪烁。

Here is my xml: 这是我的xml:

listview_bell_items.xml listview_bell_items.xml

<?xml version="1.0" encoding="utf-8"?>
   <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="fill_parent"
       android:layout_height="wrap_content"
       android:orientation="horizontal"
       android:padding="5dip" >

       <TextView
             android:id="@+id/txt_bell_title"
             android:layout_width="wrap_content"
             android:layout_height="wrap_content"
             android:layout_centerHorizontal="true"
             android:layout_centerVertical="true"
             android:text="@string/rihanna_love_the_way_lie"
             android:textColor="@drawable/bell_selected_text_color"
             android:textSize="15sp"
             android:textStyle="bold"
             android:typeface="sans" />

       <ImageView
            android:id="@+id/list_bell_image"
            android:layout_width="50dip"
            android:layout_height="50dip"
            android:src="@drawable/bronze_bell" />

  </RelativeLayout>

bell_selected_text_color.xml bell_selected_text_color.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="@color/light_yellow"/>
             <item android:state_focused="true" android:color="@color/light_yellow"/>
             <item android:state_checked="true" android:color="@color/light_yellow"/>
             <item android:state_selected="true" android:color="@color/light_yellow"/>
             <item android:color="@color/white"/>

     </selector>

1.you can use setAlpha(0 to 255 value) to set in textview so it will highlight or set opacity in android textview. 1.您可以使用setAlpha(0到255的值)在textview中进行设置,这样它将在android textview中突出显示或设置不透明度。

Use this link: http://stackoverflow.com/questions/2838757/how-to-set-opacity-alpha-for-view-in-android         

(for opacity set in textview) (对于在textview中设置的不透明度)

2.other option set blink of textview: 2.其他选项设置textview闪烁:

public static void blinkView(final TextView txtView) {

        Animation anim = new AlphaAnimation(0.0f, 1.0f);
        anim.setDuration(400); // You can manage the time of the blink with this
                                // parameter
        anim.setStartOffset(20);
        anim.setRepeatMode(Animation.REVERSE);
        anim.setRepeatCount(Animation.INFINITE);
        txtView.startAnimation(anim);
    }

this function set to blink your textview ..... 此功能设置为使您的文本视图闪烁.....

I hope its useful to you.. 希望对您有用。

The Selection class could be used to achieve your objective. Selection类可用于实现您的目标。 See the official documentation of Selection class below: 请参阅下面的Selection类的官方文档:

http://developer.android.com/reference/android/text/Selection.html http://developer.android.com/reference/android/text/Selection.html

Also, the below link could be useful: 另外,以下链接可能会很有用:

ClickableSpan TextView stays selected after click ClickableSpan TextView在单击后保持选中状态

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM