TabHost layout for Android

Source: Internet
Author: User

1.Concept

The container that holds the Tab is TabHost. There are two ways to implement TabHost:

The first method inherits TabActivity and obtains TabHost from TabActivity using the getTabHost () method. The content of each Tab can be defined in the layout file.

The second method does not inherit TabActivity. You can define TabHost in the layout file, but the id of TabWidget must be @ android: id/tabs. the id of FrameLayout must be @ android: id/tabcontent.

2. Case studies

1) inherit TabActivity

Res/layout/main. xml

<? Xml version = "1.0" encoding = "UTF-8"?>
<! -- Define the TabHost component -->
<TabHost xmlns: android = "http://schemas.android.com/apk/res/android" android: layout_width = "fill_parent" android: layout_height = "fill_parent">
<! -- Define the content of the first tab -->
<LinearLayout android: id = "@ + id/tab01" android: orientation = "vertical" android: layout_width = "fill_parent" android: layout_height = "fill_parent">
<! -- Define two textviews to display the content in the tab -->
<TextView android: layout_width = "fill_parent" android: layout_height = "wrap_content" android: text = "Sun Wukong-2011/07/12"/>
<TextView android: layout_width = "fill_parent" android: layout_height = "wrap_content" android: text = "-2011/07/10"/>
</LinearLayout>
<! -- Define the content of the second tab -->
<LinearLayout android: id = "@ + id/tab02" android: orientation = "vertical" android: layout_width = "fill_parent" android: layout_height = "fill_parent">
<TextView android: layout_width = "fill_parent" android: layout_height = "wrap_content" android: text = "Saa-2011/07/11"/>
<TextView android: layout_width = "fill_parent" android: layout_height = "wrap_content" android: text = "Tang Seng-2011/07/10"/>
</LinearLayout>
<! -- Define the content of the third tab -->
<LinearLayout android: id = "@ + id/tab03" android: orientation = "vertical" android: layout_width = "fill_parent" android: layout_height = "fill_parent">
<TextView android: layout_width = "fill_parent" android: layout_height = "wrap_content" android: text = "Sun Wukong-2011/07/12"/>
<TextView android: layout_width = "fill_parent" android: layout_height = "wrap_content" android: text = "Saa-2011/07/08"/>
</LinearLayout>
</TabHost>

HelloTabHost. java

Public class HelloTabHost extends TabActivity {

@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );

// Call the getTabHost () method of TabActivity to obtain the TabHost object
TabHost tabHost = getTabHost ();

// Set the TabHost Layout
LayoutInflater. from (this). inflate (R. layout. main, tabHost. getTabContentView (), true );

// Add the first tab
TabHost. addTab (tabHost. newTabSpec ("tab01"). setIndicator ("answered"). setContent (R. id. tab01 ));

// Add the second tab and add an image to the tab
TabHost. addTab (tabHost. newTabSpec ("tab02 "). setIndicator ("Missed call", getResources (). getDrawable (R. drawable. icon )). setContent (R. id. tab02 ));

// Add the third tab
TabHost. addTab (tabHost. newTabSpec ("tab03"). setIndicator ("dialing"). setContent (R. id. tab03 ));
}
}

Running effect:

2) do not inherit TabActivity

The <TabWidget> label id must be tabs and <FrameLayout> label id must be tabcontent. In this way, you must call the setup method after obtaining TabHost through findViewById.

Main. xml Code
<? Xml version = "1.0" encoding = "UTF-8"?>
<! -- TabHost must contain a TabWidget and a FrameLayout -->
<TabHost android: id = "@ + id/tabhost" android: layout_width = "fill_parent" android: layout_height = "wrap_content">
<LinearLayout android: orientation = "vertical" android: layout_width = "fill_parent" android: layout_height = "fill_parent">
<! -- The id attribute of TabWidget must be @ android: id/tabs -->
<TabWidget android: id = "@ android: id/tabs" android: orientation = "horizontal" android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"/>
<! -- The id attribute of FrameLayout must be @ android: id/tabcontent -->
<FrameLayout android: id = "@ android: id/tabcontent" android: layout_width = "fill_parent" android: layout_height = "fill_parent">
<TextView android: id = "@ + id/view1" android: layout_width = "fill_parent" android: layout_height = "fill_parent"/>
<TextView android: id = "@ + id/view2" android: layout_width = "fill_parent" android: layout_height = "fill_parent"/>
<TextView android: id = "@ + id/view3" android: layout_width = "fill_parent" android: layout_height = "fill_parent"/>
</FrameLayout>
</LinearLayout>
</TabHost>
</LinearLayout>

Java code

Public class TabHostTest extends Activity {
@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );
// Obtain the TabHost object
TabHost tabHost = (TabHost) findViewById (R. id. tabhost );
// If TabActivity is not inherited, use this method to load and start tabHost
TabHost. setup ();
TabHost. addTab (tabHost. newTabSpec ("tab1"). setIndicator ("first tag ",
GetResources (). getDrawable (R. drawable. icon). setContent (
R. id. view1 ));

TabHost. addTab (tabHost. newTabSpec ("tab3"). setIndicator ("third tag ")
. SetContent (R. id. view3 ));

TabHost. addTab (tabHost. newTabSpec ("tab2"). setIndicator ("second tag ")
. SetContent (R. id. view2 ));
}
}


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.