The beautification and design of Android Tabhost

Source: Internet
Author: User

The content of the layout XML file used is as follows:

<?xml version= "1.0" encoding= "Utf-8"? ><tabhost xmlns:android= "Http://schemas.android.com/apk/res/android "Android:id= "@android: Id/tabhost"Android:layout_width= "Fill_parent"Android:layout_height= "Fill_parent" > <LinearLayout android:orientation= "Vertical"Android:layout_width= "Fill_parent"Android:layout_height= "Fill_parent" > <framelayout Android:id= "@android: Id/tabcontent"Android:layout_width= "Fill_parent"Android:layout_height= "Wrap_content"Android:layout_weight= "1" > </FrameLayout> <Tabwidget Android:id= "@android: Id/tabs"Android:layout_width= "Fill_parent"Android:layout_alignparentbottom= "true"Android:layout_height= "Wrap_content"/> </LinearLayout></TabHost>

Code that is described in Activity:

//Remember to define these variables firstPrivatetabhost tabhost;PrivateTabwidget Tabwidget; Field Mbottomleftstrip; Field Mbottomrightstrip; Publictabhost Maketab () {if( This. Tabhost = =NULL) {Tabhost=(Tabhost) Findviewbyid (Android.                R.id.tabhost); Tabwidget=(Tabwidget) Findviewbyid (Android.                R.id.tabs);                Tabhost.setup ();                Tabhost.bringtofront (); Tabspec Nearby_tab= Tabhost.newtabspec ("Nearby_tab"); Tabspec History_tab= Tabhost.newtabspec ("History_tab"); Tabspec About_tab= Tabhost.newtabspec ("About_tab"); Nearby_tab.setindicator ("First", Getresources (). getdrawable (R.drawable.first)). SetContent (NewIntent ( This, first.class)); History_tab.setindicator ("Second", Getresources (). getdrawable (R.drawable.second)). SetContent (NewIntent ( This, second.class)); About_tab.setindicator ("Third", Getresources (). getdrawable (R.drawable.third)). SetContent (NewIntent ( This, third.class));                Tabhost.addtab (Nearby_tab);                Tabhost.addtab (History_tab);                Tabhost.addtab (About_tab); if(Integer.valueof (Build.VERSION.SDK) <= 7) {                        Try{Mbottomleftstrip= Tabwidget.getclass (). Getdeclaredfield ("Mbottomleftstrip"); Mbottomrightstrip= Tabwidget.getclass (). Getdeclaredfield ("Mbottomrightstrip"); if(!mbottomleftstrip.isaccessible ()) {mbottomleftstrip.setaccessible (true); }                                if(!mbottomrightstrip.isaccessible ()) {mbottomrightstrip.setaccessible (true);                                } mbottomleftstrip.set (Tabwidget, Getresources (). getdrawable (R.drawable.linee));                        Mbottomrightstrip.set (Tabwidget, Getresources (). getdrawable (R.drawable.linee)); } Catch(Exception e) {e.printstacktrace (); }                } Else {                        Try{Mbottomleftstrip= Tabwidget.getclass (). Getdeclaredfield ("Mleftstrip"); Mbottomrightstrip= Tabwidget.getclass (). Getdeclaredfield ("Mrightstrip"); if(!mbottomleftstrip.isaccessible ()) {mbottomleftstrip.setaccessible (true); }                                if(!mbottomrightstrip.isaccessible ()) {mbottomrightstrip.setaccessible (true);                                } mbottomleftstrip.set (Tabwidget, Getresources (). getdrawable (R.drawable.linee));                        Mbottomrightstrip.set (Tabwidget, Getresources (). getdrawable (R.drawable.linee)); } Catch(Exception e) {//TODO auto-generated Catch blockE.printstacktrace (); }                }                 for(inti = 0; I <tabwidget.getchildcount (); i++) {                        FinalTextView TV =(TextView) Tabwidget.getchildat (i). Findviewbyid (Android.                        R.id.title);                        Tv.settextcolor (Color.White); View VVV=Tabwidget.getchildat (i); if(Tabhost.getcurrenttab () = =i)                        {vvv.setbackgrounddrawable (Getresources (). getdrawable (R.drawable.focus)); }                        Else{vvv.setbackgrounddrawable (Getresources (). getdrawable (R.drawable.unfocus)); }} tabhost.setontabchangedlistener (NewOntabchangelistener () {@Override Public voidontabchanged (String tabId) {//TODO auto-generated Method Stub                         for(inti = 0; I < Tabwidget.getchildcount (); i++) {View VVV=Tabwidget.getchildat (i); if(Tabhost.getcurrenttab () = =i)                                {vvv.setbackgrounddrawable (Getresources (). getdrawable (R.drawable.focus)); }                                Else{vvv.setbackgrounddrawable (Getresources (). getdrawable (R.drawable.unfocus));        }                        }                        }                }); }        Else{                returnTabhost; }        return NULL;}

<ignore_js_op> testtab.zip (107.97 KB, downloaded: 778)

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.