Android [elementary tutorial] Chapter 1 TabHost Control

Source: Internet
Author: User

In this chapter, we will learn the TabHost control. Each of the four students of Tang Miao has their own skills. It is difficult to put them together for display. Now we will use TabHost to display them separately, each is a separate view. First:

 


 

Everyone saw that every one of them was a personal display. Well, let's take a look at the main. xml code:

 
<? Xml version = "1.0" encoding = "UTF-8"?>
 
<TabHost xmlns: android = "http://schemas.android.com/apk/res/android"
Android: id = "@ android: id/tabhost" android: layout_width = "match_parent"
Android: layout_height = "match_parent">
<LinearLayout android: layout_width = "match_parent"
Android: id = "@ + id/linearLayout1" android: layout_height = "match_parent"
Android: orientation = "vertical">
<TabWidget android: layout_width = "match_parent"
Android: layout_height = "wrap_content" android: id = "@ android: id/tabs"> </TabWidget>
<FrameLayout android: layout_width = "match_parent"
Android: layout_height = "match_parent" android: id = "@ android: id/tabcontent">
<LinearLayout android: layout_width = "match_parent"
Android: orientation = "vertical" android: layout_height = "match_parent"
Android: id = "@ + id/tab1">
 
<ImageView android: layout_width = "wrap_content" android: id = "@ + id/imageView1"
Android: layout_height = "wrap_content" android: src = "@ drawable/tangseng"
Android: layout_gravity = "center"> </ImageView>
<TextView android: text = "" android: layout_width = "match_parent"
Android: layout_height = "wrap_content" android: textSize = "24dp"/>
<ListView android: id = "@ + id/listView1" android: entries = "@ array/tangseng"
Android: layout_height = "wrap_content" android: layout_width = "match_parent"> </ListView>
 
</LinearLayout>
<LinearLayout android: layout_width = "match_parent"
Android: orientation = "vertical" android: layout_height = "match_parent"
Android: id = "@ + id/tab2">
 
<ImageView android: layout_width = "wrap_content" android: id = "@ + id/imageView1"
Android: layout_height = "wrap_content" android: src = "@ drawable/wukong"
Android: layout_gravity = "center"> </ImageView>
<TextView android: text = "" android: layout_width = "match_parent"
Android: layout_height = "wrap_content" android: textSize = "24dp"/>
<ListView android: id = "@ + id/listView2" android: entries = "@ array/wukong"
Android: layout_height = "wrap_content" android: layout_width = "match_parent"> </ListView>
 
</LinearLayout>
<LinearLayout android: layout_width = "match_parent"
Android: orientation = "vertical" android: layout_height = "match_parent"
Android: id = "@ + id/tab3">
 
<ImageView android: layout_width = "wrap_content" android: id = "@ + id/imageView1"
Android: layout_height = "wrap_content" android: src = "@ drawable/bajie"
Android: layout_gravity = "center"> </ImageView>
<TextView android: text = "" android: layout_width = "match_parent"
Android: layout_height = "wrap_content" android: textSize = "24dp"/>
<ListView android: id = "@ + id/listView3" android: entries = "@ array/bajie"
Android: layout_height = "wrap_content" android: layout_width = "match_parent"> </ListView>
 
</LinearLayout>
 
<LinearLayout android: layout_width = "match_parent"
Android: orientation = "vertical" android: layout_height = "match_parent"
Android: id = "@ + id/tab4">
 
<ImageView android: layout_width = "wrap_content" android: id = "@ + id/imageView1"
Android: layout_height = "wrap_content" android: src = "@ drawable/shaseng"
Android: layout_gravity = "center"> </ImageView>
<TextView android: text = "Sha monk Club" android: layout_width = "match_parent"
Android: layout_height = "wrap_content" android: textSize = "24dp"/>
<ListView android: id = "@ + id/listView4" android: entries = "@ array/shaseng"
Android: layout_height = "wrap_content" android: layout_width = "match_parent"> </ListView>
 
</LinearLayout>
</FrameLayout>
</LinearLayout>
</TabHost>
The TabHost control is defined here. The TabHost Control ID must be set to android: id = "@ android: id/tabhost". Otherwise, an error will be reported during running. next, the TabWidget control must define the ID as android: id = "@ android: id/tabs". Finally, the FrameLayout ID must be set to android: id = "@ android: id/tabcontent". There are no fewer than one of the three conditions. Otherwise, you cannot run it.

Well, let's take a look at the java source code:

 
Import android. app. TabActivity;
Import android. OS. Bundle;
Import android. widget. TabHost;
 
Public class TabHostDemoActivity extends TabActivity
{
Private String [] item = {"Tang Seng", "Sun Wukong", "", "Sha Monk "};
Private int [] tab = {R. id. tab1, R. id. tab2, R. id. tab3, R. id. tab4 };

@ Override
Protected void onCreate (Bundle savedInstanceState)
{
// TODO Auto-generated method stub
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main1 );
 
TabHost tabHost = (TabHost) findViewById (android. R. id. tabhost );

Int len = item. length;
For (int I = 0; I <len; I ++ ){
TabHost. addTab (tabHost. newTabSpec (item [I]). setIndicator (item [I]). setContent (tab [I]);
}

 
}
}
Wow, so few code? I put the data under values/string, followed by string. xml.

 
<? Xml version = "1.0" encoding = "UTF-8"?>
<Resources>
<String name = "hello"> Hello World, ButtonDemoActivity! </String>
<String name = "app_name"> ButtonDemo </string>
<String-array name = "tangseng">
<Item> mantra </item>
<Item> Amitabha </item>
</String-array>
<String-array name = "wukong">
<Item> 72 changes </item>
<Item> goblin </item>
<Item> tengyun driving fog </item>
</String-array>
<String-array name = "bajie">
<Item> lazy </item>
<Item> sleeping </item>
</String-array>
<String-array name = "shaseng">
<Item> pick the burden </item>
</String-array>
</Resources>
I directly display the ListView control through android: entries = "@ array/tangseng" the defined array, so very few java code is used in the main java code, however, for real applications, data is certainly not known and must be obtained through databases or other methods. In this chapter, we mainly learn how to use TabHost. look, the capabilities of Tang Miao's four migration teams are all at a glance.

 

From: kangkangz4 Column

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.