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