Android tabhost example

Source: Internet
Author: User

You can implement the tab effect in Android by referencing tabhost in the layout file, obtaining the tabhost instance by ID in the activity, or directly inheriting tabactivity, directly obtain the tabhost instance.

The main code of the program is as follows:
1. layout file tab. in the layout file, note that if the tabhost label references the tabwidget label, you must set its Android: Id = "@ Android: ID/tabs ", the framelayout tag ID must be set to Android: Id = "@ Android: ID/tabcontent". Otherwise, a null pointer exception occurs.

<? XML version = "1.0" encoding = "UTF-8"?>
<Tabhost xmlns: Android = "http://schemas.android.com/apk/res/android"
Android: Id = "@ + ID/tabhost"
Android: layout_width = "match_parent"
Android: layout_height = "match_parent">
<Relativelayout
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent">
<! -- Set the tab title attributes and set the title

Align the bottom -->
<Tabwidget
Android: Id = "@ Android: ID/tabs"
Android: layout_alignparentbottom = "true" // This

A statement is used to set tabwidget to the bottom of the screen, which is at the top of the screen by default.
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content">
</Tabwidget>
<! -- Set the tab body Attributes -->
<Framelayout
Android: Id = "@ Android: ID/tabcontent"
Android: layout_width = "match_parent"
Android: layout_height = "match_parent">
<Linearlayout
Android: Id = "@ + ID/tab1"
Android: Orientation = "vertical"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
Android: paddingtop = "20dip">
<Imageview

Android: layout_width = "wrap_content"

Android: layout_height = "wrap_content"

Android: layout_gravity = "center"
Android: src = "@ drawable/home"/>
</Linearlayout>
<Linearlayout
Android: Id = "@ + ID/tab2"
Android: Orientation = "vertical"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
Android: paddingtop = "20dip">
<Imageview

Android: layout_width = "wrap_content"

Android: layout_height = "wrap_content"

Android: layout_gravity = "center"

Android: src = "@ drawable/garbage"/>
</Linearlayout>
<Linearlayout
Android: Id = "@ + ID/tab3"
Android: Orientation = "vertical"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
Android: paddingtop = "20dip">
<Imageview

Android: layout_width = "wrap_content"

Android: layout_height = "wrap_content"

Android: layout_gravity = "center"
Android: src = "@ drawable/help"/>
</Linearlayout>
</Framelayout>
</Relativelayout>
</Tabhost>

Activity file tabdemo. Java

Import Android. App. activity;
Import Android. OS. Bundle;
Import Android. View. gravity;
Import Android. widget. tabhost;
Import Android. widget. Toast;
Import Android. widget. tabhost. ontabchangelistener;

Import com. KF. samples5.r;

Public class tabdemo extends activity {

@ Override
Protected void oncreate (bundle savedinstancestate ){
// Todo auto-generated method stub
Super. oncreate (savedinstancestate );
Setcontentview (R. layout. Tab );
Tabhost host = (tabhost) findviewbyid (R. Id. tabhost );
Host. Setup ();

Tabhost. tabspec homespec = host. newtabspec ("home"); // This

Param will be used as Tabid.
Homespec. setindicator (null, // This param will diplay as title.
Getresources (). getdrawable (R. drawable. home_normal ));
Homespec. setcontent (R. Id. tab1 );
Host. addtab (homespec );

Tabhost. tabspec garbagespec = host. newtabspec ("garbage ");
Garbagespec. setindicator (null, getresources (). getdrawable

(R. drawable. garbage_normal ));
Garbagespec. setcontent (R. Id. tab2 );
Host. addtab (garbagespec );

Tabhost. tabspec maybespec = host. newtabspec ("help ");
Maybespec. setindicator (null, getresources (). getdrawable

(R. drawable. help_normal ));
Maybespec. setcontent (R. Id. tab3 );
Host. addtab (maybespec );

Host. setontabchangedlistener (New ontabchangelistener (){

@ Override
Public void ontabchanged (string Tabid ){
// Todo auto-generated method stub
Toast = toast. maketext (tabdemo. This, Tabid, Toast. length_short );
Toast. setgravity (gravity. center_horizontal, 0, 50 );
Toast. Show ();
}
});

// Host. setcurrenttabbytag ("home ");
Toast = toast. maketext (tabdemo. This, "home ",

Toast. length_short );
Toast. setgravity (gravity. center_horizontal, 0, 50 );
Toast. Show ();
}
}

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.