Android Custom Tabhost Selector

Source: Internet
Author: User

As follows:


Implementation: Using the RadioButton single-selection function, we only need to change the selection and selection of the background color and font color.

Item_selet_layout file:

<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/res/ Android "Android:layout_width=" Match_parent "android:layout_height=" match_parent "android:orientation=" vertical " > <radiogroup android:id= "@+id/radiogroup" android:layout_width= "Match_parent" android:layou t_height= "wrap_content" android:orientation= "horizontal" > <radiobutton style= "@style/tabho St_left_style "android:layout_width=" match_parent "android:layout_height=" Wrap_content "a ndroid:layout_weight= "1" android:gravity= "center" android:padding= "10DP" android:text= "awaiting trial Nuclear "/> <radiobutton style=" @style/tabhost_center_style "android:layout_width=" Match_pare NT "android:layout_height=" Wrap_content "android:layout_weight=" 1 "android:gravity=" Cente R "android:padding=" 10DP"android:text=" audited "/> <radiobutton style=" @style/tabhost_right_style "Android            Oid:layout_width= "Match_parent" android:layout_height= "Wrap_content" android:layout_weight= "1" android:gravity= "center" android:padding= "10DP" android:text= "my"/> </RadioGroup> </LinearLayout>

Tabhostselector Inheritance LinearLayout:

public class Tabhostselector extends Linearlayout{public tabhostselector (context context, AttributeSet Attrs) {super ( context, attrs); init (context);} private void init (context context) {View view = Layoutinflater.from (context). Inflate (R.layout.item_select_layout, NULL ); Radiogroup Radiogroup = (radiogroup) View.findviewbyid (R.id.radiogroup); Radiogroup.setoncheckedchangelistener (new Oncheckedchangelistener () {@Overridepublic void oncheckedchanged (radiogroup group, int checkedid) {utils.showtoast ( GetContext (), checkedid+ ""); switch (checkedid) {case 1:break;case 2:break;case 3:break;default:break;}}); Radiogroup.check (2); Linearlayout.layoutparams ll = new Layoutparams (New Layoutparams (LAYOUTPARAMS.MATCH_PARENT,LAYOUTPARAMS.WRAP_ CONTENT)); This.addview (VIEW,LL);}

Because of the three options, the left and right two upper and lower corners are curved, and three style is defined, respectively, to control the angle.

 <style name= "Tabhost_center_style" parent= "@android: Style/widget.compoundbutton.radiobutton" > <item name= "Android:button" > @null </item> <item name= "android:textsize" >18sp</item> <item name= " Android:textcolor "> @color/tabhost_textcolor_selector</item> <item name=" Android:background ">@ drawable/tabhost_center_elector</item> </style> <style name= "Tabhost_left_style" parent= "@android: Style/widget.compoundbutton.radiobutton "> <item name=" Android:button "> @null </item> <item N Ame= "Android:textsize" >18sp</item> <item name= "Android:textcolor" > @color/tabhost_textcolor_ selector</item> <item name= "Android:background" > @drawable/tabhost_left_elector</item> </sty le> <style name= "Tabhost_right_style" parent= "@android: Style/widget.compoundbutton.radiobutton" > <it EM name= "Android:button" > @null </item&Gt <item name= "Android:textsize" >18sp</item> <item name= "Android:textcolor" > @color/tabhost_ textcolor_selector</item> <item name= "Android:background" > @drawable/tabhost_right_elector</item > </style>

Res/color/color/tabhost_textcolor_selector: Control text is changed to white when selected, not selected as black. Figure:

XML code:

<?xml version= "1.0" encoding= "Utf-8"? ><selector xmlns:android= "http://schemas.android.com/apk/res/ Android ">    <item android:state_checked=" true "android:color=" @android: Color/white "/><item android: Color= "@android: Color/black" ></item></selector> here to set the font color will have a lot of problems, beginning in other ways to achieve, always invalid, and later see this way, can be achieved. Create a new color folder under the Res folder and place the font color selector file inside.

The transformation of the background color:


Tabhost_right_elector.xml

<?xml version= "1.0" encoding= "Utf-8"? ><selector xmlns:android= "http://schemas.android.com/apk/res/ Android ">    <item android:state_checked=" true "><shape>            <solid android:color=" @color/ Tabhost_selected "/>            <stroke android:width=" 1DP "android:color=" @color/tabhost_selected "/>            < Corners android:bottomrightradius= "2DP" android:toprightradius= "2DP"/>        </shape></item>    <item android:state_checked= "false" ><shape>            <solid android:color= "@android: Color/white"/>            <corners android:bottomrightradius= "2DP" android:toprightradius= "2DP"/>            <stroke android:width= " 1DP "android:color=" @color/tabhost_selected "/>        </shape></item></selector>



Android Custom Tabhost Selector

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.