TabHost of Android Control

Source: Internet
Author: User

Create an Android project tabHost with the package name com. test. www.

Man layout file, with some irrelevant textviews, you can remove these textviews

<? Xml version = "1.0" encoding = "UTF-8"?>
<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
Android: orientation = "vertical">
<! -- This is the same as text_id -->
<TextView
Android: id = "@ + id/tip_id"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: text = "tip"
Android: textColor = "#850"
Android: gravity = "center"
Android: layout_gravity = "center"/>
<TabHost
Android: id = "@ android: id/tabhost"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent">
<! -- This is the display in the center -->
<TextView
Android: id = "@ + id/textRed_id"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: text = "textred"
Android: textcolor = "# f00"
Android: gravity = "center"
Android: layout_gravity = "center"/>
<! -- This is the vertical center of the side -->
<Textview
Android: Id = "@ + ID/textgreen_id"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: text = "textgreen"
Android: textcolor = "#0f0"
Android: layout_gravity = "center"/>
<! -- This is the vertical center of edges on the tabhost -->
<Textview
Android: Id = "@ + ID/textblue_id"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: text = "textblue"
Android: textcolor = "# 00f"
/>
<Linearlayout
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
Android: Orientation = "vertical">
<Textview
Android: Id = "@ + ID/text_id"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: text = "text"
Android: textcolor = "# F50"
Android: gravity = "center"
Android: layout_gravity = "center"/>
<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"
Android: padding = "5dp">
</FrameLayout>

</Linearlayout>
</Tabhost>

</Linearlayout>

The following activity has only one textview. Click it to enter the tabactivity.

Package com. Test. WWW;

Import Android. App. activity;
Import Android. content. intent;
Import Android. OS. Bundle;
Import Android. View. view;
Import Android. View. View. onclicklistener;
Import Android. widget. textview;

Public class tablehostactivity extends activity {
/** Called when the activity is first created .*/
@ Override
Public void oncreate (bundle savedinstancestate ){
Super. onCreate (savedInstanceState );
// SetContentView (R. layout. main );
TextView TV = new TextView (this );
TV. setText ("table host! ");
TV. setTextSize (30 );
TV. setOnClickListener (new OnClickListener (){

Public void onClick (View v ){
// TODO Auto-generated method stub
Startactivity (new intent (tablehostactivity. This, activitytablehost. Class ));
Finish ();
}
});
Setcontentview (TV );
}
}

The following is tabactivity (the activity of the tab inherits this class). Click textview in this class to display the title of the tab on another interface,

Package com. Test. WWW;

Import com. Test. www. R;

Import Android. App. tabactivity;
Import Android. content. intent;
Import Android. content. res. Resources;
Import Android. OS. Bundle;
Import Android. util. log;
Import Android. widget. tabhost;
Import android. widget. TabHost. OnTabChangeListener;

Public class ActivitytableHost extends TabActivity {
@ Override
Protected void onCreate (Bundle savedInstanceState ){
// TODO Auto-generated method stub
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );
Resources res = getResources ();

TabHost tabHost = getTabHost ();
// Spec statement, speculative
TabHost. TabSpec spec;

Intent intent;
// Indicator
Intent = new intent (this, activityinside. Class). putextra ("show", "Tab 1"); // pass the value to the startup Interface
// Name; characters to be displayed; animation settings
Spec = tabhost. newtabspec ("A1"). setindicator ("A1", res. getdrawable (R. drawable. C1 ))
. Setcontent (intent); // set an acitivity

Tabhost. addtab (SPEC );

Intent = new intent (this, activityinside. Class). putextra ("show", "Tab 2 ");

Spec = tabHost. newTabSpec ("a2"). setIndicator ("a2", res. getDrawable (R. drawable. c1 ))
. SetContent (intent );

TabHost. addTab (spec );

Intent = new Intent (this, ActivityInside. class). putExtra ("show", "tab 3 ");

Spec = tabHost. newTabSpec ("a3"). setIndicator ("a3", res. getDrawable (R. drawable. c1 ))
. SetContent (intent); // The name and icon of the tab

Tabhost. addtab (SPEC );

Intent = new intent (this, activityinside. Class). putextra ("show", "tab 4 ");

Spec = tabhost. newtabspec ("A4"). setindicator ("A4", res. getdrawable (R. drawable. C1 ))
. Setcontent (intent );

Tabhost. addtab (SPEC );

Tabhost. setcurrenttab (2); // give an initial value, pointing to this position

Tabhost. setontabchangedlistener (New ontabchangelistener (){

Public void ontabchanged (string Tabid ){
// Todo auto-generated method stub

}
});
}
}
This is the tab page that displays the tabhost tab. Click the tab title above to display the tab. The program exits.

Package com. Test. WWW;

Import Android. App. activity;
Import Android. OS. Bundle;
Import Android. View. view;
Import Android. View. View. onclicklistener;
Import android. widget. TextView;

Public class ActivityInside extends Activity {
@ Override
Protected void onCreate (Bundle savedInstanceState ){
// TODO Auto-generated method stub
Super. onCreate (savedInstanceState );
TextView TV = new TextView (this );
TV. setOnClickListener (new OnClickListener (){

Public void onClick (View v ){
// TODO Auto-generated method stub
Finish ();
}
});
TV. setText (getIntent (). getStringExtra ("show "));
SetContentView (TV );
}
}

Configuration File

<? Xml version = "1.0" encoding = "UTF-8"?>
<Manifest xmlns: android = "http://schemas.android.com/apk/res/android"
Package = "com. test. www"
Android: versionCode = "1"
Android: versionName = "1.0" type = "codeph" text = "/codeph">

<Uses-sdk android: minSdkVersion = "4"/>

<Application
Android: icon = "@ drawable/ic_launcher"
Android: Label = "@ string/app_name">
<Activity
Android: Name = ". tablehostactivity"
Android: Label = "@ string/app_name">
<Intent-filter>
<Action Android: Name = "android. Intent. Action. Main"/>

<Category Android: Name = "android. Intent. Category. launcher"/>
</Intent-filter>
</Activity>
<Activity android: name = "ActivitytableHost"> </activity>
<Activity android: name = "ActivityInside"> </activity>
</Application>

</Manifest>
Running Effect

Related Article

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.