Android UI Control Series: Tabwidget (switch card)

Source: Internet
Author: User
TAB tab similar to the Phone book interface, through multiple tags to switch between different content, to achieve this effect, first of all, you know tabhost, it is a container to hold multiple tab tags, each tab can correspond to their own layout, for example, the tab layout in the phone book is a linear layout

To use Tabhost, first get the Tabhost object through the Gettabhost method, then add tab to Tabhost through the AddTab method, and of course each tab will produce an event to capture this event when switching. You need to set the Tabactivity event listener Setontabchangedlistener

Here's a small example:

Tabtest.java

Package Org.hualang.tab;import Android.app.activity;import Android.app.tabactivity;import android.graphics.Color; Import Android.os.bundle;import Android.widget.tabhost;import Android.widget.toast;import Android.widget.tabhost.ontabchangelistener;public class Tabtest extends Tabactivity {/** Called when the activity is F Irst created.    */Tabhost Tabhost;        @Override public void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);        Setcontentview (R.layout.main);        Get Tabhost Object tabhost = Gettabhost (); Add a tag for tabhost//Create a new Newtabspec (NEWTABSPEC)//Set its label and icon (setindicator)//set content (SetContent) Tabhos T.addtab (Tabhost.newtabspec ("Tab1"). Setindicator ("tab 1", Getresources (). Getdrawable (r.drawable.img        1). SetContent (R.ID.TEXT1)); Tabhost.addtab (Tabhost.newtabspec ("TaB2"). Setindicator ("tab 2", Getresources (). getdrawable (R.DRAWABLE.IMG2) )               . SetContent (R.ID.TEXT2)); Tabhost.addtab (Tabhost.newtabspec ("Tab3"). Setindicator ("Tab 3", Getresources (). getdrawable (R.DRAWABLE.IMG3)        ). SetContent (R.ID.TEXT3));        Sets the background color of the Tabhost//tabhost.setbackgroundcolor (Color.argb (150,22,70,150));        Set Tabhost background Image resource Tabhost.setbackgroundresource (R.DRAWABLE.BG0);        Sets which label is currently displayed Tabhost.setcurrenttab (0);                Tag switch event handling, Setontabchangedlistener Tabhost.setontabchangedlistener (new Ontabchangelistener () { public void ontabchanged (String tabId) {Toast Toast=toast.maketext (getapplication                        Context (), "Now is the" +tabid+ "label", Toast.length_short);                Toast.show ();    }        }); }}

Main.xml

<?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_hei ght= "Fill_parent" > <tabwidget android:id= "@android: Id/tabs" android:layout_width= "Fill_ Parent "android:layout_height=" wrap_content "/> <framelayout android:id=" @android: Id/ta Bcontent "android:layout_width=" fill_parent "android:layout_height=" Fill_parent "> < TextView android:id= "@+id/text1" android:layout_width= "Fill_parent" android:l ayout_height= "fill_parent" android:text= tab 1 "/> <textview android:id=" @+i D/text2 "Android:lAyout_width= "Fill_parent" android:layout_height= "fill_parent" android:text= "tab 2"/>                <textview android:id= "@+id/text3" android:layout_width= "Fill_parent" android:layout_height= "fill_parent" android:text= tab 3 "/> </FrameLayout> </lin Earlayout></tabhost>

The results are as follows:

The above is the Android UI Control series: Tabwidget (switch card) content, more relevant content please pay attention to topic.alibabacloud.com (www.php.cn)!

  • 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.