Android--tabwidget

Source: Internet
Author: User

1,Activity_tabwidget.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:layout_width= "Fill_parent"
android:layout_height= "Fill_parent"
android:orientation= "Vertical" >

<tabwidget
Android:id= "@android: Id/tabs"
Android:layout_width= "Fill_parent"
android:layout_height= "Wrap_content" >
</TabWidget>

<framelayout
Android:id= "@android: Id/tabcontent"
Android:layout_width= "Fill_parent"
android:layout_height= "Fill_parent" >

<textview
Android:id= "@+id/TB_TV1 "
Android:layout_width= "Fill_parent"
android:layout_height= "Fill_parent"
Android:text= "This is the first tab"/>

<textview
Android:id= "@+id/tb_tv2"
Android:layout_width= "Fill_parent"
android:layout_height= "Fill_parent"
Android:text= "This is another tab"/>

<textview
Android:id= "@+id/tb_tv3"
Android:layout_width= "Fill_parent"
android:layout_height= "Fill_parent"
Android:text= "This is the Third tab"/>
</FrameLayout>
</LinearLayout>

</TabHost>

2,Tabwidgetactivity.java

public class Tabwidgetactivity extends Tabactivity {
Declaring Tabhost objects
tabhost tabhost;

@SuppressWarnings ("deprecation")
@Override
protected void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.activity_tabwidget);
try {

Get Tabhost Object
tabhost = Gettabhost ();
/ * Add tags to tabhost * /
//Create a new Newtabspec (NEWTABSPEC)
//Set its label and icon (Setindicator)
//Setup content (setcontent)
Tabhost.addtab (tabhost
.Newtabspec("Tab_test1")
.Setindicator("TAB1",
Getresources (). getdrawable (R.drawable.button1))
.setcontent(R.ID.TB_TV1));
Tabhost.addtab (tabhost
. Newtabspec ("Tab_test2")
. Setindicator ("TAB2",
Getresources (). getdrawable (R.drawable.button2))
. SetContent (R.ID.TB_TV2));
Tabhost.addtab (Tabhost.newtabspec ("Tab_test3"). Setindicator ("TAB3",
Getresources (). getdrawable (R.drawable.button3)). SetContent (R.ID.TB_TV3));

Set the background color of tabhost
Tabhost.setbackgroundcolor (Color.argb (150, 22, 70, 150));
Set the background image resource for Tabhost
Tabhost.setbackgroundresource (R.drawable.ic_launcher);
Set the currently displayed label
Tabhost.Setcurrenttab(0);
} catch (Exception e) {
E.printstacktrace ();
}
Label Switching event handling
Tabhost.Setontabchangedlistener(New Ontabchangelistener () {

@Override
public void ontabchanged (String tabId) {
Dialog Dialog = new Alertdialog.builder (tabwidgetactivity.this)
. Settitle ("hint")
. Setmessage ("currently checked" + tabId + "label")
. Setpositivebutton ("OK",
New Dialoginterface.onclicklistener () {

@Override
public void OnClick (Dialoginterface dialog,
int Whichbutton) {
Dialog.cancel ();
}
}). Create ();
Dialog.show ();
}
});

}
}

Android--tabwidget

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.