Tabhost for Android

Source: Internet
Author: User

The tab is a frequently used interface control during interface design. It allows you to quickly switch between multiple pages and display different content on each page.
Use of the tab
1. First, design the page layout of some pages.
2. After the paging design is complete, use the code to create a tab and add the logo and title to each tab.
3. Finally, determine the page layout displayed on each page.
Create an XML file for each page to edit and save the page layout. The method used is no different from the method used to design a common user interface.

General steps for using the tab
First, design the page layout.
Activity inherits tabactivity
Call the gettabhost () method of tabactivity to obtain the tabhost object.
Create a tab using tabhost

 

The so-called tabhost provides the window view container of the tab page. this object contains two sub-objects: one is a set of tags that allow users to select a specified tab, and the other is framelayout used to display the tab content. some elements in the tab are generally controlled by their container objects, rather than directly setting the values of the child element.

First demonstrate a simple example, there is only one activity, but note that it inherits the tabactivity class.

Step 1: input the code in Main. XML in the layout file. Note that framelayout is used here. The main feature is that it can overwrite other content and only display the current

View code

1 <? XML version = "1.0" encoding = "UTF-8"?>
2
3 <framelayout xmlns: Android = "http://schemas.android.com/apk/res/android"
4 Android: Id = "@ + ID/framelayout01"
5 Android: layout_width = "wrap_content"
6 Android: layout_height = "wrap_content">
7
8 <textview
9 Android: Id = "@ + ID/textview01"
10 Android: layout_width = "wrap_content"
11 Android: layout_height = "wrap_content"
12 Android: text = "all call records"> </textview>
13
14 <textview
15 Android: Id = "@ + ID/textview02"
16 Android: layout_width = "wrap_content"
17 Android: layout_height = "wrap_content"
18 Android: text = "incoming calls"> </textview>
19
20 <textview
21 Android: Id = "@ + ID/textview03"
22 Android: layout_width = "wrap_content"
23 Android: layout_height = "wrap_content"
24 Android: text = "missed calls"> </textview>
25
26 </framelayout>

Step 2: Compile the layouttab01activity. Java file, which is relatively simple.

View code

1 package cn.edu. zwu. Tel;
2
3 Import Android. App. tabactivity;
4 Import Android. OS. Bundle;
5 import Android. View. layoutinflater;
6 Import Android. widget. tabhost;
7 Import Android. widget. Toast;
8 Import Android. widget. tabhost. ontabchangelistener;
9
10
11 public class layouttab01activity extends tabactivity {
12 @ override
13 public void oncreate (bundle savedinstancestate ){
14 super. oncreate (savedinstancestate );
15
16 tabhost th = gettabhost ();
17 // declare the tabhost and use layoutinflater to filter out the layout. Add the framelayout containing the tab page to the tabhost.
18 // from (this) Get layoutinflater from this tabactivity
19 // R. layout. Main stores the tab Layout
20 // use tabhost to obtain the framelayout that stores the tab content
21 // whether to tie inflate to the root layout element
22 layoutinflater. From (this). Inflate (R. layout. Main, Th. gettabcontentview (), true );
23 // use tabhost to obtain the framelayout that stores the tab content,
24 // newtabspecd is used to obtain a new tabhost. tabspec and associate it with the current tabhost.
25 // setindicator is used to specify labels and icons as Tab indicators.
26 // setcontent specifies the view ID used to display the tab content.
27 th. addtab (Th. newtabspec ("all "). setindicator ("all call records", getresources (). getdrawable (R. drawable. call_forward )). setcontent (R. id. textview01 ));
28 th. addtab (Th. newtabspec ("OK "). setindicator ("Incoming call", getresources (). getdrawable (R. drawable. phone_call )). setcontent (R. id. textview02 ));
29 th. addtab (Th. newtabspec ("cancel "). setindicator ("Missed call", getresources (). getdrawable (R. drawable. call_bluetooth )). setcontent (R. id. textview03 ));
30 // The setontabchangelistener job registers a callback function, which is called when the selected status of any tab changes.
31 th. setontabchangedlistener (
32 new ontabchangelistener (){
33 @ override
34 public void ontabchanged (string Tabid ){
35 toast. maketext (layouttab01activity. This, Tabid, Toast. length_long). Show ();
36}
37}
38 );
39}
40}

 

Demo:

 

Method 2: The layout of framelayout is not used, but listview is used directly. The creattabcontent () callback function of the tab content is created.

Step 1: Compile the main. xml file. The Code is as follows:

View code

 1 <?xml version="1.0" encoding="utf-8"?>
2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
3 android:layout_width="fill_parent"
4 android:layout_height="fill_parent"
5 android:orientation="vertical" >
6
7 <TextView
8 android:layout_width="fill_parent"
9 android:layout_height="wrap_content"
10 android:text="@string/hello" />
11
12 </LinearLayout>

Step 2: Write the layouttabactivity. Java file. The specific explanations are all commented in the code.

View code

1 package cn.edu. zwu. Tel;
2
3 Import java. util. arraylist;
4 Import java. util. List;
5 import Android. App. tabactivity;
6 Import Android. OS. Bundle;
7 Import Android. View. view;
8 Import Android. widget. arrayadapter;
9 Import Android. widget. listview;
10 Import Android. widget. tabhost;
11
12 // tabcontentfactory indicates that the content of a tab is generated when a tab is selected.
13 // if the content of your tab is generated according to certain conditions, use this interface. For example, if the existing view is not displayed, the activity is started.
14 public class layouttab02activity extends tabactivity implements tabhost. tabcontentfactory {
15
16 @ override
17 Public void oncreate (bundle savedinstancestate)
18 {
19 super. oncreate (savedinstancestate );
20 tabhost th = gettabhost ();
21 // newtabspecd is used to obtain a new tabhost. tabspec and associate it with the current tabhost.
22 // setindicator is used to specify labels and icons as Tab indicators.
23 // setcontent specifies the view ID used to display the tab content.
24 th. addtab (Th. newtabspec ("all"). setindicator ("all call records"). setcontent (this ));
25 th. addtab (Th. newtabspec ("OK"). setindicator ("Incoming call"). setcontent (this ));
26 th. addtab (Th. newtabspec ("cancel"). setindicator ("Missed call"). setcontent (this ));
27}
28 // create a callback function for the tab content.
29 public view createtabcontent (string tag)
30 {
31 listview Lv = new listview (this );
32 list <string> List = new arraylist <string> ();
33 List. Add (TAG );
34 if (tag. Equals ("all "))
35 {
36 List. Add ("Tom ");
37 list. Add ("kite ");
38 list. Add ("Rose ");
39} else if (tag. Equals ("OK "))
40 {
41 list. Add ("Tom ");
42 list. Add ("kite ");
43} else
44 {
45 list. Add ("Rose ");
46}
47
48 arrayadapter <string> adapter = new arrayadapter <string> (this,
49 Android. R. layout. simple_list_item_checked, list );
50 LV. setadapter (adapter );
51 return lv;
52}
53}

:

 

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.