Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android UI之Tab

Android UI之Tab

編輯:關於Android編程

Tab標簽頁是UI設計時經常使用的UI控件,可以實現多個分頁之間的快速切換,每個分頁可以顯示不同的內容。

TabHost相當於浏覽器中標簽頁分布的集合,而Tabspec則相當於浏覽器中的每一個分頁面。在Android中,每一個TabSpec分布可以是一個組件,也可以是一個布局,然後將每一個分頁裝入TabHost中,TabHost即可將其中的每一個分頁一並顯示出來。

使用Tab標簽頁的一般步驟:
首先要設計所有的分頁的界面布局
Activity繼承TabActivity
調用TabActivity的getTabHost()方法獲得TabHost對象
通過TabHost創建並添加Tab

實例:TabDemo
運行效果:
\
代碼清單:
布局文件:tab.xml

<frameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    

    

    

</frameLayout>

Java源代碼文件: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知識點
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 should be used as the content of the tab.


  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved