Android UI Tab (Tabactivity+tabhost implementation)

Source: Internet
Author: User

The Tab tab is a UI control that is often used in UI design, enabling quick transitions between multiple pages, each of which can display different content.

Tabhost corresponds to a collection of tab distributions in the browser, while Tabspec is the equivalent of every page in the browser. In Android, each tabspec distribution can be a component or a layout, and then each page is loaded into the Tabhost, Tabhost can display each of them together.

General steps for using the Tab tab page:
The first thing to do is to design all of the paging interface layouts
Activity Inheritance Tabactivity
Call Tabactivity's Gettabhost () method to get the Tabhost object
Create and Add tab through Tabhost

Example: TabDemo
Operating effect:

Code Listing:
Layout file: Tab.xml
<?xml version= "1.0" encoding= "Utf-8"? ><framelayout xmlns:android= "http://schemas.android.com/apk/res/ Android "    android:layout_width=" fill_parent "    android:layout_height=" fill_parent ">    <textview Android:id= "@+id/view1"        android:background= "@drawable/blue"        android:layout_width=        "Fill_parent" android:layout_height= "Fill_parent"        android:text= "Here is the content of TAB1. "/>    <textview android:id=" @+id/view2 "        android:background=" @drawable/red "        android:layout_width = "Fill_parent"        android:layout_height= "fill_parent"        android:text= "here is Tab2"/>    <textview Android:id= "@+id/view3"        android:background= "@drawable/green" android:layout_width= "Fill_parent"        android:layout_height= "fill_parent"        android:text= "TAB3"/></framelayout>

Java source code file: Mainactivity.java
Package Com.rainsong.tabdemo;import Android.app.tabactivity;import Android.os.bundle;import Android.view.layoutinflater;import Android.widget.tabhost;public class Mainactivity extends TabActivity{    Tabhost Mtabhost;    /** called when the activity is first created. */    @Override public    void OnCreate (Bundle savedinstancestate)    {        super.oncreate (savedinstancestate) ;        Mtabhost = Gettabhost ();        Layoutinflater.from (This). Inflate (R.layout.tab,                Mtabhost.gettabcontentview (), true);        Mtabhost.addtab (Mtabhost.newtabspec ("Tab1"). Setindicator ("Tab1")                . SetContent (R.id.view1));        Mtabhost.addtab (Mtabhost.newtabspec ("Tab3"). Setindicator ("Tab2")                . SetContent (R.ID.VIEW2));        Mtabhost.addtab (Mtabhost.newtabspec ("Tab3"). Setindicator ("Tab3")                . SetContent (R.ID.VIEW3));}    }



API Knowledge Points
public class
tabactivity
Extends Activitygroup

Tabhost Gettabhost ()
Returns the Tabhost the activity is a using to host its tabs.

Public abstract class
Layoutinflater
Extends Object

Class Overview
Instantiates a layout XML file into its corresponding View objects.

Static Layoutinflater from (context context)
Obtains the layoutinflater from the given context.

View Inflate (int resource, ViewGroup root, Boolean attachtoroot)
Inflate a new view hierarchy from the specified XML resource.

public class
Tabhost
Extends Framelayout
Implements Viewtreeobserver.ontouchmodechangelistener

Known Direct subclasses
Fragmenttabhost

Class Overview

Container for a tabbed window view. This object holds the CHILDREN:A set of tab labels, the user clicks to select a specific tab, and a framelayout objec T that displays the contents of the page.


Framelayout Gettabcontentview ()
Get the framelayout which holds tab content

void AddTab (Tabhost.tabspec tabspec)
Add a tab.

Tabhost.tabspec Newtabspec (String tag)
Get a new tabhost.tabspec associated with the This tab host.

public class
Tabhost.tabspec
Extends Object

Class Overview
A tab has a tab indicator, content, and a tag which is used to keep track of it.
This builder helps choose among these options.
For the tab indicator, your choices is:
1) Set a label
2) set a label and an icon
For the tab content, your choices is:
1) The ID of a View
2) A tabhost.tabcontentfactory that creates the View content.
3) An Activity in Intent that launches.

Tabhost.tabspec Setindicator (charsequence label)
Specify a label as the tab indicator.


Tabhost.tabspec setcontent (int viewId)

Specify the ID of the view that should is used as the content of the tab.


Android UI Tab (Tabactivity+tabhost implementation)

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.