I. Tabhost INTRODUCTION
The Tabhost component can store multiple tabs in the interface, and many of the software uses a modified component for design;
1. Tabhost Common Components
tabwidget : This component is the Tabhost tab in the upper or lower buttons, you can click the button Switch tab;
Tabspec : Represents the Tab interface, add a tabspec can be added to the tabhost;
-- Create tab : Newtabspec (String tag), create a tab;
-- Add tab : AddTab (TABSPEC);
2. Tabhost Use steps
A. defining layouts : using the Tabhost component in an XML file and defining a framelayout tab content in it;
B. inherit tabactivity : Show tab component Activity inheritance tabactivity;
C. get the component : Get the Tabhost object by calling the Gettabhost () method ;
D. Create Add Tab : Create Add tab via Tabhost;
3. Place the button below
The tabwidget in the layout file represents the tab button, and the Fragement component represents the content;
setting Failure Condition : If the Fragement component does not have the Android:layout_weight property set, the Tabwidget is placed below and the button may not appear;
set weight : After you set the weight of the fragment component, you can successfully display the tab button;
Two. Tabhost Layout file
1. Root tag and ID
set the android ID : XML layout file, you can use the tag settings, where the ID needs to refer to Android's own Id:android:[email Protected]:id/tabhost;
GetHost () Gets the premise : After setting the ID, the activity interface can use GetHost () to get the Tabhost view object;
Example:
?
| 1 |
<tabhost android:id="@android:id/tabhost"android:layout_height="match_parent"android:layout_width="match_parent"></tabhost> |
2. Tabwidget Components
tab Toggle : The component is a tab toggle button that can be toggled by clicking on the Component tab;
set Android ID : The ID of this component should be set to Android's own Id:android:[email Protected]:id/tabs;
Tabhost Prerequisites : This component and the Framelayout component are the two components required in the Tabhost component;
The toggle button displays below : If you want to place the button below, you can define the component below, but note thatframelayout to set android:layout_widget = 1;
set Tabwidget size : If you want to set the size of the button component, you can set the weight of the component and the Framelayout component;
Example:
?
| 1 |
<tabwidget android:id="@android:id/tabs"android:layout_height="wrap_content" android:layout_width="fill_parent"android:orientation="horizontal/"></tabwidget> |
3. Framelayout Components
Component Action : The subcomponents defined in the component are the tabs that are displayed for each page in the Tabhost, and you can define the view that the Tabhost tab displays;
set Android ID : The ID of this component should be set to Android's own Id:android:[email protected]:id/tabcontent;
Example:
?
| 1 |
<framelayout android:id="@android:id/tabcontent"android:layout_height="fill_parent" android:layout_weight="1"android:layout_width="fill_parent"></framelayout> |
Two. Activity methods
1. Get Tabhost
Get method : GetHost ();
Premise : The method of calling the GetHost () method to get the Tabhost component is the premise that the ID android:[email protected]:id/tabhost of the android comes in the layout file;
2. Create a tab
Create tab : Invokes the Newtabhost (tag) of the Tabhost component, where the tag is a string, which is the unique identifier on the tab;
setting tab :
-- Set button name : Setindicator (called Beast);
-- Set tab content : SetContent (), you can set the view components, you can set activity, you can also set fragement;
add tab : Tabhost.add (TAG), the passed-in parameter is the unique identifier defined when the tab is created;
Three codes
XML layout file :
?
| 1234567891011121314151617181920212223242526 |
<!--?xml version=1.0 encoding=utf-8?--><tabhost android:id="@android:id/tabhost" android:layout_height="match_parent" android:layout_width="match_parent" xmlns:android="http://schemas.android.com/apk/res/android"> <linearlayout android:layout_height="fill_parent" android:layout_width="fill_parent" android:orientation="vertical"> <tabwidget android:id="@android:id/tabs" android:layout_height="wrap_content" android:layout_width="fill_parent" android:orientation="horizontal/"> <framelayout android:id="@android:id/tabcontent" android:layout_height="fill_parent" android:layout_weight="1" android:layout_width="fill_parent"> <linearlayout android:id="@+id/alwayswet" android:layout_height="fill_parent" android:layout_width="fill_parent" android:orientation="vertical"> <imageview android:layout_height="fill_parent" android:layout_width="fill_parent" android:scaletype="fitXY" android:src="@drawable/alwayswet/"> </imageview></linearlayout> <linearlayout android:id="@+id/isanimal" android:layout_height="fill_parent" android:layout_width="fill_parent" android:orientation="vertical"> <imageview android:layout_height="fill_parent" android:layout_width="fill_parent" android:scaletype="fitXY" android:src="@drawable/isanimal/"> </imageview></linearlayout> <linearlayout android:id="@+id/nezha" android:layout_height="fill_parent" android:layout_width="fill_parent" android:orientation="vertical"> <imageview android:layout_height="fill_parent" android:layout_width="fill_parent" android:scaletype="fitXY" android:src="@drawable/nazha/"> </imageview></linearlayout> </framelayout> </tabwidget></linearlayout> </tabhost> |
Activity Main Interface Code :
?
| 123456789101112131415161718192021222324252627282930313233 |
package shuliang.han.tabhost_test; import android.app.TabActivity;import android.os.Bundle;import android.widget.TabHost;import android.widget.TabHost.TabSpec;public class MainActivity extends TabActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.tabhost); TabHost tabHost = getTabHost(); TabSpec page1 = tabHost.newTabSpec(tab1) .setIndicator(叫兽) .setContent(R.id.isanimal); tabHost.addTab(page1); TabSpec page2 = tabHost.newTabSpec(tab2) .setIndicator(老湿) .setContent(R.id.alwayswet); tabHost.addTab(page2); TabSpec page3 = tabHost.newTabSpec(tab3) .setIndicator(哪吒) .setContent(R.id.nezha); tabHost.addTab(page3); }} |
Reprint "android-tabhost tab function usage detailed"