How to Use the Tab 2 (Android learning essay 13)

Source: Internet
Author: User

 

1. Do not inherit TabActivity

 

2. Define TabHost in the layout

 

Note: The TabWidget id must be @ android: id/tabs, and the FrameLayout id must be @ android: id/tabcontent.

 

Java code:

 

 

Package yc. demo;

 

Import android. app. Activity;

Import android. OS. Bundle;

Import android. widget. TabHost;

Import android. widget. TabHost. TabSpec;

 

Public class TabhostDemo1Activity extends Activity {

/** Called when the activity is first created .*/

@ Override

Public void onCreate (Bundle savedInstanceState ){

Super. onCreate (savedInstanceState );

SetContentView (R. layout. demo1 );

// Obtain the TabHost

TabHost tabHost = (TabHost) this. findViewById (R. id. tabs );

TabHost. setup ();

// Obtain a new TabHost. TabSpec and associate it with the current tabHost.

// Parameter: Required tab labels

TabSpec pSpes = tabHost. newTabSpec ("parent ");

PSpes. setIndicator ("parent class", this. getResources (). getDrawable (R. drawable. msg_icon ));

PSpes. setContent (R. id. textView1 );

TabSpec subSpec = tabHost. newTabSpec ("sub ");

SubSpec. setIndicator ("subclass", this. getResources (). getDrawable (R. drawable. at_icon ));

SubSpec. setContent (R. id. textView2 );

// Add a tab to tabHost

TabHost. addTab (pSpes );

TabHost. addTab (subSpec );

}

}

Xml layout file:

 

 

<? 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">

 

<TabHost android: id = "@ + id/tabs"

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"/>

<FrameLayout android: id = "@ android: id/tabcontent"

Android: layout_height = "fill_parent"

Android: layout_width = "fill_parent">

<TextView android: id = "@ + id/textView1"

Android: layout_height = "fill_parent"

Android: layout_width = "fill_parent"

Android: text = "parent"/>

<TextView android: id = "@ + id/textView2"

Android: layout_height = "fill_parent"

Android: layout_width = "fill_parent"

Android: text = "sub"/>

</FrameLayout>

</LinearLayout>

</TabHost>

 

</LinearLayout>

 

Tip: The display order of tabs is determined based on the order in which you add tabs.



From tianshijianbing1989

 

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.