Android Tabhost for tab switching

Source: Internet
Author: User

Tabhost is the entire tab container, contains the Tabwidget and framelayout two parts, Tabwidget is the expression of each tab (icon effect), Framelayout is the tab content

There are two ways of implementing this:

1. Inheriting tabactivity

2. Inheriting Activity class

Method One: Inherit Tabactivity

Get Tabhost from tabactivity with the Gettabhost () method and set the label contents

Layout:

1, tabhost must be set Android:id to @android:id/tabhost
2, Tabwidget must be set Android:id to @android:id/tabs
3, framelayout must be set Android:id to @android:id/tabcontent

These are the system comes with the ID, preferably the shortcut key Lenovo generation, do not hand-written, so it is not easy to make mistakes

XML layout file:

1 <Tabhostxmlns:android= "Http://schemas.android.com/apk/res/android"2 Android:layout_width= "Match_parent"3 Android:layout_height= "Match_parent"4  android:id= "@android: Id/tabhost" 5     >6 7     <LinearLayout8         Android:layout_width= "Match_parent"9 Android:layout_height= "Match_parent"Ten android:orientation= "vertical" One         > A  -  -  the         <Framelayout -             Android:layout_width= "Match_parent" - Android:layout_height= "0DP" - Android:layout_weight= "1" +  android:id= "@android: id/tabcontent"  -             > +             <LinearLayout A                 Android:layout_width= "Match_parent" at Android:layout_height= "Match_parent" - Android:id= "@+id/widget_layout_red" - Android:background= "#ff0000" - android:orientation= "vertical" -                 ></LinearLayout> -  in             <LinearLayout -                 Android:layout_width= "Match_parent" to Android:layout_height= "Match_parent" + Android:id= "@+id/widget_layout_yellow" - Android:background= "#FCD209" the android:orientation= "vertical" *                 ></LinearLayout> $ Panax Notoginseng         </Framelayout> -         <Tabwidget the             Android:layout_width= "Match_parent" + Android:layout_height= "Wrap_content"41  android:id= "@android: Id/tabs"  the Android:background= "@mipmap/ic_launcher" +             > -  $         </Tabwidget> $     </LinearLayout> - </Tabhost>

Java Code Implementation:

1 Public class Mainactivity extends Tabactivity {2 private Tabhost tabhost;3 @Override4 protected void OnCreate (Bundle savedinstancestate) {5 super.oncreate (savedinstancestate);6 Setcontentview (r.layout.activity_main);7 8 //Get tab tabhost from Tabactivity9 tabhost = Gettabhost ();Ten  One Tabhost.addtab (Tabhost A //Create new label One - . Newtabspec ("one") - //Set label title the . Setindicator ("Red") - //Set the layout content of the label - . SetContent (r.id.widget_layout_red)); - tabhost.addtab ("Tabhost.newtabspec"). Setindicator ("Yellow"). SetContent (R.id.widget_layout_yellow)); +     } -}

The implementation results are as follows:

Method Two: Inherit the Activity class

Layout:

1, tabhost can be customized ID
2, Tabwidget must be set Android:id to @android:id/tabs
3, framelayout must be set Android:id to @android:id/tabcontent

XML layout:

<Tabhostxmlns:android= "Http://schemas.android.com/apk/res/android"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent" android:id= "@+id/zidingyi"     >    <LinearLayoutAndroid:layout_width= "Match_parent"Android:layout_height= "Match_parent"android:orientation= "vertical"        >        <FramelayoutAndroid:layout_width= "Match_parent"Android:layout_height= "0DP"Android:layout_weight= "1"Android:id= "@android: Id/tabcontent"            >            <LinearLayoutAndroid:layout_width= "Match_parent"Android:layout_height= "Match_parent"Android:id= "@+id/widget_layout_red"Android:background= "#ff0000"android:orientation= "vertical"                ></LinearLayout>            <LinearLayoutAndroid:layout_width= "Match_parent"Android:layout_height= "Match_parent"Android:id= "@+id/widget_layout_yellow"Android:background= "#FCD209"android:orientation= "vertical"                ></LinearLayout>        </Framelayout>        <TabwidgetAndroid:layout_width= "Match_parent"Android:layout_height= "Wrap_content"Android:id= "@android: Id/tabs"Android:background= "@mipmap/ic_launcher"            >        </Tabwidget>    </LinearLayout></Tabhost>

Java Code Implementation:

public class Mainactivity extends Activity {    private tabhost tabhost;    @Override    protected void onCreate (Bundle savedinstancestate) {        super.oncreate (savedinstancestate);        Setcontentview (r.layout.activity_main);        Get Tabhost Object instance        tabhost = (tabhost) Findviewbyid (r.id.ho);        Call Tabhost.setup ()        tabhost.setup ();        Create tab Tag        Tabhost.addtab (Tabhost.newtabspec ("one"). Setindicator ("Red"). SetContent (r.id.widget_layout_red)) ;        Tabhost.addtab ("Tabhost.newtabspec"). Setindicator ("Yellow"). SetContent (R.id.widget_layout_yellow));}    }

Android Tabhost for tab switching

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.