Android Development Tabactivity Usage examples detailed _android

Source: Internet
Author: User

This example describes the Tabactivity usage of Android development. Share to everyone for your reference, specific as follows:

I. INTRODUCTION

Tabactivity inherits from the activity in order to allow the same interface to accommodate more content. Tabactivity to implement the function of the tab page, through the navigation bar to manage each page.

Two. xml layout file

Attention:

The layout file for 1.TabActivity requires tabhost as the root of the XML layout file.

2. Usually we use a linear layout, so the child element of <TabHost> is <LinearLayout>.

3.<tabwidget> corresponding Tab
<FrameLayout> is used to include what the TAB key needs to show
It is important to note that the IDs for <TabWidget> and <FrameLayout> must use the system ID, android:id/tabs and android:id/tabcontent respectively.
Because the system uses two IDs to initialize the Tabhost two instance variables (Mtabwidget and mtabcontent).

4. code example

<?xml version= "1.0" encoding= "Utf-8"?> <tabhost android:id=
"@android: Id/tabhost" xmlns:android= "
        Http://schemas.android.com/apk/res/android "
        android:layout_width=" match_parent "
        android:layout_height= "Match_parent" >
  <linearlayout
        android:orientation= "vertical"
        android:layout_width= "Match_" Parent "
        android:layout_height=" match_parent >
  <tabwidget
        android:id= "@android: Id/tabs"
        android:layout_width= "match_parent"
        android:layout_height= "wrap_content" >
  </tabwidget >
  <framelayout android:id= "@android: id/tabcontent"
       android:layout_width= "Match_parent"
       android:layout_height= "Match_parent" >
</FrameLayout>
</LinearLayout>
</ Tabhost>

Three. tabactivity

1.tabhost:tabhost is the tab's carrier, which is used to manage tab.

Some functions of 2.TabHost

(1) obtain

Tabhost Tabhost=this.gettabhost ();

(2) Create Tabhost.tabspec

Public Tabhost.tabspec Newtabspec (String tag)

(3) Add tab

public void AddTab (Tabhost.tabspec tabspec)

(4) Remove all the tabs

public void Clearalltabs () public
int Getcurrenttab ()

(5) Set the current tab (by index)

public void Setcurrenttab (int index)

(6) Set the current (Tab by tag)

public void Setcurrenttabbytag (String tag)

(7) Set up response processing for tabchanged event

public void Setontabchangedlistener (Tabhost.ontabchangelistener l)

3.tabhost.tabspec to set the tab label and content, you need to set the Tabhost.tabspec class. Tabhost.tabspec Management:

Public String Gettag () public
tabhost.tabspec setcontent public
tabhost.tabspec Setindicator

(1) Indicator here the indicator is the label on the tab and it can

Set Label:

Setindicator (Charsequence label)

Set label and icon:

Setindicator (charsequence label, drawable icon)

Specify a view:

Setindicator (View view)

(2) Content for content, is the contents of the tab, you can

Set the ID of the view:

setcontent (int viewid)

Use new Intent to introduce the contents of other activity: SetContent (Intent Intent)

Package com.zhanglong.music;
Import android.app.TabActivity;
Import android.content.Intent;
Import android.content.res.Resources;
Import Android.os.Bundle;
Import Android.view.Window;
Import Android.view.WindowManager;
Import Android.widget.TabHost; The public class Mainactivity extends Tabactivity {/** called ' when the ' activity is ' is a. */@Override Public V
    OID onCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);
    Requestwindowfeature (Window.feature_no_title); This.getwindow (). SetFlags (WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_
    fullscreen);
    Setcontentview (R.layout.main);
    Resources res = getresources ();
    Tabhost tabhost = Gettabhost ();
    Tabhost.tabspec spec;
    Intent Intent;
    Intent = new Intent (). SetClass (this, listactivity.class); Spec = Tabhost.newtabspec ("Music"). Setindicator ("Music", Res.getdrawable (R.drawable.item)). SetContent (i
    Ntent); Tabhost.addtab (SPEC);
    Intent = new Intent (). SetClass (this, artistsactivity.class); Spec = Tabhost.newtabspec ("artist"). Setindicator ("Artist", Res.getdrawable (R.drawable.artist)). Setconte
    NT (Intent);
    Tabhost.addtab (spec);
    Intent = new Intent (). SetClass (this, albumsactivity.class); Spec = Tabhost.newtabspec ("album"). Setindicator ("album", Res.getdrawable (R.drawable.album)). SetContent (
    Intent);
    Tabhost.addtab (spec);
    Intent = new Intent (). SetClass (this, songsactivity.class); Spec = Tabhost.newtabspec ("recently played"). Setindicator ("Recently played", Res.getdrawable (R.drawable.album)). Setcont
    ENT (Intent);
    Tabhost.addtab (spec);
  Tabhost.setcurrenttab (0);

 }
}

For more information on Android-related content readers can view the site topics: "Android Development Introduction and Advanced Course", "Android Multimedia operating skills Summary (audio, video, recording, etc.)", "Android Basic Components Usage Summary", " Android View tips Summary, Android layout layout tips and a summary of Android controls usage

I hope this article will help you with the Android program.

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.