Android uses selector to modify the font color and background color in TextView _android

Source: Internet
Author: User

This article illustrates how Android uses selector to modify the font color and background color in TextView. Share to everyone for your reference, specific as follows:

Android Selector Everyone is familiar with it can be easily implemented, control in different actions, color equivalence changes. Here I say some of the applications in TextView.

I think we all know that button buttons in the Source view is a special kind of textview, so many times, the button is all used TextView to complete, as long as a android:clickable= "true" on it.

TextView in the use of selector, there will be two cases, one is normal in the TextView control to judge the action of press, focus, and other actions, the other is TextView the outer control of these actions, and then the action back to TextView.

One, the normal in the TextView control to judge Press, focus and other actions of judgment

This is relatively simple, generally to modify the control background color and text color, we need to use two XML files. The code is as follows:

Tbackground.xml Modify Background

<?xml version= "1.0" encoding= "Utf-8"?> <selector xmlns:android=
"http://schemas.android.com/apk/res/" Android >
  <!--default background picture-->
  <!--<item android:drawable= "@color/white"/>-->
  <! --background picture with no focus-->
  <item android:state_window_focused= "false" android:drawable= "@color/white"/>
  <item android:state_focused= "false" android:state_pressed= "true"  android:drawable= "@color/btnbackblue"/ >
</selector>

Here's a point, as you can see, I gave the default background picture (color) to the note, which is why, because you put this on the front, no matter when, it will be the first run, it ran, the program will not run down again, so the following is not written. If you want to set the default value, please put this line of code to the bottom.

Ttextcolor.xml Modify Text

<?xml version= "1.0" encoding= "Utf-8"?> <selector xmlns:android=
"http://schemas.android.com/apk/res/" Android ">
  <!--background picture with no focus-->
  <item android:state_window_focused=" false android:color= "@color /black "/>
  <item android:state_focused=" false "android:state_pressed=" true "  android:color=" @color/ White "/>
  <!--default background picture-->
  <item android:color=" @color/black "/>
</selector>

Text changes I put the default value, put to the end, here also want to say, the background we want to use android:drawable and the color of the text to be used Android:color, or it will be an error, why? Let's think about it. Ha ha....

<textview
    android:id= "@+id/txt_collection_cancel"
    android:layout_width= "0DP"
    android:layout_ height= "Fill_parent"
    android:layout_weight= "1"
    android:text= "Cancel"
    android:textcolor= "@drawable Ttextcolor "
    android:gravity=" center "
    android:background=" @drawable/tbackground "
    android: Clickable= "true"/>

Second, TextView the actions of the outer control, and then returns the action back to TextView.

This often happens, and we usually add a linearlayout or relativelayout to the outer layer. And we will put the clicked event to this outer control. At this point, you want to modify the background of the outer control, and the text color of the TextView control. This time, we also use the above way, you will find that TextView no response, why, because it did not get the event, this time, will use a property is Android:duplicateparentstate

Its official explanation is "if you set this property, you will get the drawing state directly from the parent container (the cursor, press inferior). Note Just get the drawing state without getting the event, which is when you click on the LinearLayout button has the effect of being clicked, but does not perform a click event. Look at the following code:

 <relativelayout android:id=" @+id/rela_collection_add "android:layout_width=" fi Ll_parent "android:layout_height=" 50DP "android:background=" @drawable/tbackground "android:clickable=" true "
    ; <view android:id= "@+id/line_collection_add" android:layout_width= "Fill_parent" android:layout_height= "1DP" android:background= "@color/gray" android:layout_gravity= "center_vertical" Android:layout_alignpare Ntbottom= "true"/> <textview android:id= @+id/txt_collection_add "android:layout_width=" fill_p 
      Arent "android:layout_height=" Fill_parent android:text= "New Favorites" android:textcolor= "@drawable/ttextcolor" Android:textsize= "@dimen/actionbar_title_size" android:duplicateparentstate= "true" android:gravity= "cen ter "android:layout_above=" @+id/line_collection_add/> </RelativeLayout> 

While modifying the background of the outer control, we are also modifying the color of the TextView text.

I hope this article will help you with the Android program.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.