Android implementation to set the underline selection effect on the Navigation tab

Source: Internet
Author: User

Technical staff core competitiveness or technology AH. Efforts to improve the implementation of various results. Oh, come on!

look directly at the effect . This linearlayout has only two buttons, and the Button1,button1 has an underline selection effect. The Buton2,button2 has an underline selected effect.

Such as.

 PackageCom.daoge.ui;Importroboguice.activity.RoboActivity;ImportRoboguice.inject.InjectView;Importandroid.graphics.drawable.Drawable;ImportAndroid.os.Bundle;ImportAndroid.view.View;ImportAndroid.view.View.OnClickListener;ImportAndroid.widget.Button;Importandroid.widget.LinearLayout;Importcom.daoge.utils.DensityUtils;ImportCom.daoge.utils.ResourceReader;ImportCOM.ROBOGUICETEST.R; Public classMainactivityextendsRoboactivityImplementsOnclicklistener {@InjectView (r.id.btntab001) Button btnTab001;    @InjectView (r.id.btntab002) Button btnTab002;    @InjectView (R.id.layout_tab) LinearLayout layout_tab; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);        Setcontentview (r.layout.test_layout);    Initallviews (); }    Private voidinitallviews () {Btntab001.setonclicklistener ( This); Btntab002.setonclicklistener ( This); }     Public voidOnClick (View v) {Switch(V.getid ()) { Caser.id.btntab001:settabselected (btnTab001);  Break;  Caser.id.btntab002:settabselected (btnTab002);  Break; default :                 Break; }    }    Private voidsettabselected (Button btnselected) {drawable selecteddrawable= Resourcereader.readdrawable ( This, R.drawable.shape_nav_indicator); intScreenWidth = Densityutils.getscreensize (mainactivity. This) [0]; intright = SCREENWIDTH/2; Selecteddrawable.setbounds (0, 0, right, DENSITYUTILS.DIPTOPX ( This, 3)); Btnselected.setselected (true); Btnselected.setcompounddrawables (NULL,NULL,NULL, selecteddrawable); intSize =Layout_tab.getchildcount ();  for(inti = 0; i < size; i++) {            if(Btnselected.getid ()! =Layout_tab.getchildat (i). GetId ()) {Layout_tab.getchildat (i). setselected (false); (Button) Layout_tab.getchildat (i)). Setcompounddrawables (NULL,NULL,NULL,NULL); }        }    }}

The most important place is here.

Private voidsettabselected (Button btnselected) {drawable selecteddrawable= Resourcereader.readdrawable ( This, R.drawable.shape_nav_indicator); intScreenWidth = Densityutils.getscreensize (mainactivity. This) [0]; intright = SCREENWIDTH/2; Selecteddrawable.setbounds (0, 0, right, DENSITYUTILS.DIPTOPX ( This, 3)); Btnselected.setselected (true); Btnselected.setcompounddrawables (NULL,NULL,NULL, selecteddrawable); intSize =Layout_tab.getchildcount ();  for(inti = 0; i < size; i++) {            if(Btnselected.getid ()! =Layout_tab.getchildat (i). GetId ()) {Layout_tab.getchildat (i). setselected (false); (Button) Layout_tab.getchildat (i)). Setcompounddrawables (NULL,NULL,NULL,NULL); }        }    }

Get this drawable, then where does this drawable come from??

R.drawable.shape_nav_indicator

Look at this drawable.shape file.

<?XML version= "1.0" encoding= "Utf-8"?><Shapexmlns:android= "Http://schemas.android.com/apk/res/android"Android:shape= "Rectangle" >    <SolidAndroid:color= "@color/global_red" />    <sizeAndroid:width= "1000DP" />    <paddingAndroid:bottom= "2DP"Android:left= "5DP"Android:right= "5DP"Android:top= "2DP" /></Shape>

Demo: Click to enter my Baidu network disk download

Android implementation to set the underline selection effect on the Navigation tab

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.