Android tabhost part

Source: Internet
Author: User

This article combinesSource codeAnd instances to describe tabhost usage.

You can use tabhost to switch between different la s on a single screen, for example, the dial-up application that comes with Android ,:
 

View the tabhost SourceCodeMain instance variables include:

Private tabwidget mtabwidget;
Private framelayout mtabcontent;
Private list <tabspec> mtabspecs
That is to say, our tabhost must have these three items, so our. xml file will have a rule: continue to view the source code:

If (mtabwidget = NULL ){
Throw new runtimeexception (
"Your tabhost must have a tabwidget whose ID attribute is 'android. R. Id. Tabs '");
}

 

Mtabcontent = (framelayout) findviewbyid (COM. Android. Internal. R. Id. tabcontent );
If (mtabcontent = NULL ){
Throw new runtimeexception (
"Your tabhost must have a framelayout whose ID attribute is 'android. R. Id. tabcontent '");
}
That is to say, the tabwidget and framelayout labels are required for our. xml file.

Next we will build our own tab instance:

There are two ways to achieve this:
One is to inherit the tabactivity class. You can use the. xml resource file defined internally by android as a container file. That is, use gettabhost (); in our code, and the corresponding background source code is as follows:

This. setcontentview (COM. Android. Internal. R. layout. tab_content );
You can see this layout in the system resource file.

 

With the container, We need to allocate content for each tab. Of course, what type of label should be:
For example, we can build a. xml file.
First, tab1.xml is a linearlayout layout.

<? XML version = "1.0" encoding = "UTF-8"?>

<Linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android"
Android: Id = "@ + ID/linearlayout01" Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content">
<Textview Android: text = "tab1 with linear layout"
Android: Id = "@ + ID/textview01" Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content">
</Textview>
</Linearlayout>


Then tab2.xml is a framelayout layout.

<? XML version = "1.0" encoding = "UTF-8"?>
<Framelayout xmlns: Android = "http://schemas.android.com/apk/res/android"

Android: Id = "@ + ID/framelayout02"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content">
<Linearlayout Android: Id = "@ + ID/linearlayout02"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content">
<Textview Android: text = "tab2"
Android: Id = "@ + ID/textview01" Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content">
</Textview>
</Linearlayout>

</Framelayout>
Then register the two framelayout content as tabhost, that is, the following code:

Layoutinflater inflater_tab1 = layoutinflater. From (this );
Inflater_tab1.inflate (R. layout. tab1, mtabhost. gettabcontentview ());
Inflater_tab1.inflate (R. layout. tab2, mtabhost. gettabcontentview ());

Then we need to build the content corresponding to the third instance variable of the tabhost mentioned above. The source code is as follows:

Private list <tabspec> mtabspecs = new arraylist <tabspec> (2 );
The space with two tabs is initialized and then automatically expanded:
Let's build our tabspec:

Mtabhost. addtab (mtabhost. newtabspec ("tab_test1"). setindicator ("tab 11"). setcontent (R. Id. linearlayout01 ));
Mtabhost. addtab (mtabhost. newtabspec ("tab_test1"). setindicator ("tab 11"). setcontent (R. Id. framelayout02 ));

That is to say, we use our two layout as their content. Of course, there can be other layout in framelayout to put my components.
We do not need to set setcontentview () in the Code; Because gettabhost (); this method has been set after being called, source code:

If (mtabhost = NULL) {
This. setcontentview (COM. android. internal. r. layout. tab_content);
}< br> that is, the system uses tab_content as the view setting.
run the following code:
complete code:

Tabhost mtabhost = gettabhost ();
Layoutinflater inflater_tab1 = layoutinflater. From (this );
Inflater_tab1.inflate (R. layout. tab1, mtabhost. gettabcontentview ());
Inflater_tab1.inflate (R. layout. tab2, mtabhost. gettabcontentview ());
Mtabhost. addtab (mtabhost. newtabspec ("tab_test1"). setindicator ("tab 11"). setcontent (R. Id. linearlayout01 ));
Mtabhost. addtab (mtabhost. newtabspec ("tab_test1"). setindicator ("tab 11"). setcontent (R. Id. framelayout02 ));

 

Another way is to define our own tabhost: do not inherit tabactivity

First, create our own. xml file. Of course, it must contain tabhost, tabwidget, and framelayout, with three labels:

<? XML version = "1.0" encoding = "UTF-8"?>
<Tabhost xmlns: Android = "http://schemas.android.com/apk/res/android"
Android: Id = "@ + ID/tabhost"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent">
<Linearlayout
Android: Orientation = "vertical"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent">
<Tabwidget
Android: Id = "@ Android: ID/tabs"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"/>
<Framelayout
Android: Id = "@ Android: ID/tabcontent"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent">

</Framelayout>
</Linearlayout>
</Tabhost>

Note: except that the tabhost ID can be customized, the System ID must be used for others. Why,
Of course, you can add the view in framelayout as the tab content only when you create tabspce, to separate the content of each tab, we still use the two tab XML files used earlier.
Java code:
Get tabhost through findviewbyid,

Setcontentview (R. layout. Main );
Tabhost mtabhost = (tabhost) findviewbyid (R. Id. tabhost );
The next important step is to use tabhost. Setup ();
The role is to initialize our tabhost container:
The source code is as follows:

<P> call setup () before adding tabs if loading tabhost using findviewbyid (). <I> <B> however </I> </B>: You do
* Not need to call setup () after gettabhost () in {@ link Android. App. tabactivity }.
That is to say, through findviewbyid, The tabhost obtained by the method must be set up, but not through gettabhost.
What does setup do: source code

Mtabwidget = (tabwidget) findviewbyid (COM. Android. Internal. R. Id. Tabs );
If (mtabwidget = NULL ){
Throw new runtimeexception (
"Your tabhost must have a tabwidget whose ID attribute is 'android. R. Id. Tabs '");
}

mtabcontent = (framelayout) findviewbyid (COM. android. internal. r. id. tabcontent);
If (mtabcontent = NULL) {
throw new runtimeexception (
"Your tabhost must have a framelayout whose ID attribute is 'android. r. id. tabcontent' ");
}< br> it initializes two instance variables of tabhost, the reason why our ID must use the system-defined ID is also answered here
the next step is the same as the previous step:

Layoutinflater inflater_tab1 = layoutinflater. From (this );
Inflater_tab1.inflate (R. layout. tab1, mtabhost. gettabcontentview ());
Inflater_tab1.inflate (R. layout. tab2, mtabhost. gettabcontentview ());
Mtabhost. addtab (mtabhost. newtabspec ("tab_test1"). setindicator ("tab A"). setcontent (R. Id. linearlayout01 ));
Mtabhost. addtab (mtabhost. newtabspec ("tab_test2"). setindicator ("tab B"). setcontent (R. Id. framelayout02 ));

Complete code:

Setcontentview (R. layout. Main );
Tabhost mtabhost = (tabhost) findviewbyid (R. Id. tabhost );
Mtabhost. Setup ();
Layoutinflater inflater_tab1 = layoutinflater. From (this );
Inflater_tab1.inflate (R. layout. tab1, mtabhost. gettabcontentview ());
Inflater_tab1.inflate (R. layout. tab2, mtabhost. gettabcontentview ());
Mtabhost. addtab (mtabhost. newtabspec ("tab_test1"). setindicator ("tab A"). setcontent (R. Id. linearlayout01 ));
Mtabhost. addtab (mtabhost. newtabspec ("tab_test2"). setindicator ("tab B"). setcontent (R. Id. framelayout02 ));

The running result is the same as the preceding one. If you have any questions, please submit them.

Reprinted, please describe the source...

You can download the source code:/files/freeman1984/atab.rar.

 

This article from the csdn blog, reproduced please indicate the source: http://blog.csdn.net/lastsweetop/archive/2010/05/07/5566200.aspx

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.