Android Custom Tabhost,tabwidget Style

Source: Internet
Author: User

Interface is relatively simple, want to do a good job to change a few pictures can be.

The first step is to put the tabhost in the layout (which is generated automatically when Main.xml is created), as long as the Tabhost control is placed on the screen:

<?XML version= "1.0" encoding= "Utf-8"?><Tabhostxmlns:android= "Http://schemas.android.com/apk/res/android"Android:id= "@+id/taskdescribe_buildingmeter_tabhost"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"Android:background= "@color/white" >    <LinearLayoutAndroid:layout_width= "Match_parent"Android:layout_height= "Match_parent"android:orientation= "vertical">                <TabwidgetAndroid:id= "@android: Id/tabs"Android:layout_width= "Match_parent"Android:layout_height= "Wrap_content">          </Tabwidget>        <FramelayoutAndroid:id= "@android: Id/tabcontent"Android:layout_width= "Match_parent"Android:layout_height= "0DP"Android:layout_weight= "1">            <LinearLayoutAndroid:id= "@+id/buildingmeter_layout_unitname"Android:layout_width= "Fill_parent"Android:layout_height= "Fill_parent"android:orientation= "vertical"Android:background= "@color/white">            </LinearLayout>            <LinearLayoutAndroid:id= "@+id/buildingmeter_layout_installstatus"Android:layout_width= "Fill_parent"Android:layout_height= "Fill_parent"android:orientation= "vertical"Android:background= "@color/white">            </LinearLayout>            <LinearLayoutAndroid:id= "@+id/buildingmeter_layout_storey"Android:layout_width= "Fill_parent"Android:layout_height= "Fill_parent"android:orientation= "vertical"Android:background= "@color/white">            </LinearLayout>                    </Framelayout>    </LinearLayout></Tabhost>

Step Two: Create a layout that shows this Tabwidget tab item Tabmini.xml:

<?XML version= "1.0" encoding= "Utf-8"?><LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Android:layout_width= "Match_parent"Android:layout_height= "40dip"Android:paddingleft= "5dip"Android:paddingright= "5dip">    <TextViewAndroid:id= "@+id/tab_lable"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"android:gravity= "Center"Android:text= "TextView" /></LinearLayout>

Step three: Implement Tabhost in activity:

Private tabhost tabhost; Private Tabwidget Tabwidget;
Layoutinflater Inflater =Activity.getlayoutinflater (); View View1= Inflater.inflate (R.layout.tabhost_tag,NULL); TextView txt1=(TextView) View1.findviewbyid (r.id.tab_lable); Txt1.settext ("By unit name"); View View2= Inflater.inflate (R.layout.tabhost_tag,NULL); TextView txt2=(TextView) View2.findviewbyid (r.id.tab_lable); Txt2.settext ("By State"); View VIEW3= Inflater.inflate (R.layout.tabhost_tag,NULL); TextView Txt3=(TextView) View3.findviewbyid (r.id.tab_lable); Txt3.settext ("By Floor"); //get Tabhost Object instanceTabhost =(tabhost) Activity.findviewbyid (r.id.taskdescribe_buildingmeter_tabhost);//call Tabhost.setup ()Tabhost.setup (); Tabwidget=tabhost.gettabwidget ();//Create tab LabelsTabhost.addtab (Tabhost.newtabspec ("one"). Setindicator (View1). SetContent (R.id.buildingmeter_layout_unitname)); Tabhost.addtab (Tabhost.newtabspec ( "Both"). Setindicator (View2). SetContent (R.id.buildingmeter_layout_installstatus)); Tabhost.addtab ( Tabhost.newtabspec ("Three"). Setindicator (VIEW3). SetContent (R.id.buildingmeter_layout_storey)); //set the label that appears by default when you first open itTabhost.setcurrenttab (0);//Initialize the color of the tab, and the color of the fontUpdatetab (tabhost);//Select ListenerTabhost.setontabchangedlistener (NewTabchangedlistener ());
/*** Update tab label color, and font color *@paramTabhost*/    Private voidUpdatetab (Finaltabhost Tabhost) {         for(inti = 0; I < Tabhost.gettabwidget (). Getchildcount (); i++) {View View=tabhost.gettabwidget (). Getchildat (i); TextView TV=(TextView) Tabhost.gettabwidget (). Getchildat (i). Findviewbyid (r.id.tab_lable); Tv.settextsize (16); Tv.settypeface (Typeface.serif,0);//setting fonts and Styles            if(Tabhost.getcurrenttab () = =i) {//selectedView.setbackground (Getresources (). getdrawable (r.drawable.tabhost_current));//the background after selectionTv.settextcolor ( This. Getresources (). Getcolorstatelist (Android.            R.color.white)); }             Else             {                //do not selectView.setbackground (Getresources (). getdrawable (R.drawable.tabhost_default));//non-selectable backgroundTv.settextcolor ( This. Getresources (). Getcolorstatelist (Android.            R.color.black)); }        }    }        /*** Tabhost Select Listener *@author      *     */    Private classTabchangedlistenerImplementsOntabchangelistener {@Override Public voidontabchanged (String tabId) {Tabhost.setcurrenttabbytag (tabId);          Updatetab (Tabhost); }    }

Android Custom Tabhost,tabwidget Style

Related Article

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.