Android UI Tab

Source: Internet
Author: User

Android UI Tab
The Tab is a commonly used UI control during uidesign. It enables fast switching between multiple pages, and different content can be displayed on each page.

TabHost is equivalent to the set of tab distribution in the browser, while Tabspec is equivalent to each paging area in the browser. In Android, each TabSpec distribution can be a component or layout, and then each page is loaded into the TabHost. The TabHost will display each page.

Follow these steps to use the Tab:
First, design the page layout.
Activity inherits TabActivity
Call the getTabHost () method of TabActivity to obtain the TabHost object.
Create and add a Tab using TabHost

Instance: TabDemo
Running effect:

Code List:
Layout file: tab. xml

 <FrameLayout xmlns: android = "http://schemas.android.com/apk/res/android" android: layout_width = "fill_parent" android: layout_height = "fill_parent">
     
     
 </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 point
Public class
TabActivity
Extends ActivityGroup

TabHost getTabHost ()
Returns the TabHost the activity is 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 two children: a set of tab labels that the user clicks to select a specific tab, and a FrameLayout object that displays the contents of that 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 this tab host.

Public class
TabHost. TabSpec
Extends Object

Class Overview
A tab has a tab indicator, content, and a tag that is used to keep track of it.
This builder helps choose among these options.
For the tab indicator, your choices are:
1) set a label
2) set a label and an icon
For the tab content, your choices are:
1) the id of a View
2) a TabHost. TabContentFactory that creates the View content.
3) an Intent that launches an Activity.

TabHost. TabSpec setIndicator (CharSequence label)
Specify a label as the tab indicator.


TabHost. TabSpec setContent (int viewId)

Specify the id of the view that shocould be used as the content of the tab.


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.