In the control with (inherit) textview, the text color of textview is dynamically changed in the code.

Source: Internet
Author: User
Tags getcolor

Today, due to the company's project requirements, we need to implement a tab similar to the tab. At that time, we directly thought of using radiogroup and radiobutton. This method is completely correct. However, in the subsequent development process, we encountered some small difficulties that had been plagued for a long time. The general requirement is: in the code, dynamically obtain the number of tabs, and then initialize radiogroup. Each tab corresponds to a radiobutton, that is, to add a radiobutton to the radiogroup, then, you need to change the text color when the button is selected. Because it is dynamically added, radiobutton and Selector cannot be configured in XML to change the text color.

Some code is attached below:

Int size = lvl1.size (); For (INT I = 0; I <size; I ++) {question q = lvl1.get (I); radiobutton BTN = getradiobutton (Q, i); radiogroup. addview (BTN, new radiogroup. layoutparams (radiogroup. layoutparams. match_parent, radiogroup. layoutparams. match_parent, 1); if (I = 0) {sublevel. clear (); sublevel. addall (Q. getsublevel (); adapter. notifydatasetchanged (); // The data changes to BTN. setchecked (true );}}

Let's take a look at the above Code. I use getradiobutton () to dynamically generate a radiobutton, and then call addview () to add it to the radiogroup. Note that:

 radioGroup.addView(btn,                    new RadioGroup.LayoutParams(RadioGroup.LayoutParams.MATCH_PARENT,                            RadioGroup.LayoutParams.MATCH_PARENT, 1));
The layout parameter of radiobutton is specified in the method. Note that you must use the radiogroup. layoutparams class to specify the layout parameter of radiobutton. If you use another example:

Linearlayout. layoutparams, viewgroup. layoutparams, and other classes will not work !!

This is my first small pitfall. Why? Because in radiogroup, it has rewritten layoutparams. At that time, I probably looked at the source code. It is estimated that there was a change in the source code.

Let's take a look at the getradiobutton method:

Private radiobutton getradiobutton (question Q, int index) {radiobutton BTN = new radiobutton (context); BTN. setid (INDEX); drawable d = context. getresources (). getdrawable (R. drawable. radiogroup_tab_selector); D. setbounds (0, 25,150, 55); BTN. setbuttondrawable (R. drawable. transparent); BTN. setcompounddrawables (null, d); BTN. settext (Q. getqs_content (); BTN. setgravity (gravity. center_horizonta L | gravity. bottom);/* use selector in the code to change the text color of the selected button. getcolorstatelist (int id) is required. This method is used to parse the selector defined and getcolor () is used () it cannot be parsed. */BTN. settextcolor (context. getresources (). getcolorstatelist (R. color. acs_tab_textcolor_selector); Return BTN ;}
Note the following two points in the above Code:

1. When you call setbuttondrawables (), you must first call the setbounds () method of drawable so that the Set drawable can be displayed.

2. in the code, use selector to dynamically change the text color when radiobutton is selected. Use getcolorstatelist (int id) to parse the selector definition and use getcolor () it cannot be parsed.


Summary: Although I used radiobutton as an example above, as long as the control inherits textview, You need to dynamically change the text color in the code.


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.