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

Source: Internet
Author: User

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.

Package Com.daoge.ui;import Roboguice.activity.roboactivity;import Roboguice.inject.injectview;import Android.graphics.drawable.drawable;import Android.os.bundle;import Android.view.view;import Android.view.view.onclicklistener;import Android.widget.button;import Android.widget.linearlayout;import Com.daoge.utils.densityutils;import Com.daoge.utils.resourcereader;import Com.roboguicetest.r;public Class    Mainactivity extends Roboactivity implements Onclicklistener {@InjectView (r.id.btntab001) Button btnTab001;    @InjectView (r.id.btntab002) Button btnTab002;    @InjectView (R.id.layout_tab) LinearLayout layout_tab;        @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);        Setcontentview (r.layout.test_layout);    Initallviews ();        } private void Initallviews () {Btntab001.setonclicklistener (this);    Btntab002.setonclicklistener (this); } public void OnClick (View v) {switch (V.getid ()) {Case r.id.btntab001:settabselected (btnTab001);            Break                Case r.id.btntab002:settabselected (btnTab002);            Break        Default:break; }} private void settabselected (Button btnselected) {drawable selecteddrawable = resourcereader.readdrawable        (This, r.drawable.shape_nav_indicator);        int screenwidth = densityutils.getscreensize (Mainactivity.this) [0];        int right = SCREENWIDTH/2;        Selecteddrawable.setbounds (0, 0, right, DENSITYUTILS.DIPTOPX (this, 3));        Btnselected.setselected (TRUE);        Btnselected.setcompounddrawables (NULL, NULL, NULL, selecteddrawable);        int size = Layout_tab.getchildcount ();                for (int i = 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 code is here:

private void settabselected (Button btnselected) {        drawable selecteddrawable = resourcereader.readdrawable (This, R.drawable.shape_nav_indicator);        int screenwidth = densityutils.getscreensize (Mainactivity.this) [0];        int right = SCREENWIDTH/2;        Selecteddrawable.setbounds (0, 0, right, DENSITYUTILS.DIPTOPX (this, 3));        Btnselected.setselected (true);        Btnselected.setcompounddrawables (NULL, NULL, NULL, selecteddrawable);        int size = Layout_tab.getchildcount ();        for (int i = 0; i < size; i++) {            if (Btnselected.getid ()! = Layout_tab.getchildat (i). GetId ()) {                layout_tab.getc Hildat (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"? ><shape xmlns:android= "Http://schemas.android.com/apk/res/android"    android:shape= "Rectangle" >    <solid android:color= "@color/global_red"/>    <size android: Width= "1000DP"/>    <padding        android:bottom= "2DP"        android:left= "5DP"        android:right= "5DP"        android:top= "2DP"/></shape>

the Demo:http://pan.baidu.com/s/1jGGG0gm

Other great article articles

Using Autocompletetext in android Dialog
Large Web site architecture design-SOLR
MySQL Hash index
Android Learning Note (32) grid view (GridView) and graphics switcher (Imageswi ...
Android Learning Note (31) Expandable List component (Expandablelistview)

More about Android development articles


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.