Tab的学习和使用

本文详细介绍了Android中TabHost组件的使用方法,包括如何创建Tab、设置Tab指示器及内容等,并提供了一个完整的示例代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

TabHost
  那么我们要用到的Tab载体是TabHost,需要从TabActivity.getTabHost获取。
  现在看看TabHost类,它有3个内嵌类:1个类TabHost.TabSpec,2个接口TabHost.TabContentFactory和TabHost.OnTabChangeListener。后面会介绍这些类和接口。
 
  TabHost类的一些函数:
  public void addTab (TabHost.TabSpec tabSpec) 添加tab,参数TabHost.TabSpec通过下面的函数返回得到
  public TabHost.TabSpec newTabSpec (String tag) 创建TabHost.TabSpec
  
  public void clearAllTabs () remove所有的Tabs
  public int getCurrentTab ()
  public String getCurrentTabTag ()
  public View getCurrentTabView ()
  public View getCurrentView ()
  public FrameLayout getTabContentView () 返回Tab content的FrameLayout
 
  public TabWidget getTabWidget ()
  public void setCurrentTab (int index)       设置当前的Tab by index
  public void setCurrentTabByTag (String tag) 设置当前的Tab by tag
  public void setOnTabChangedListener (TabHost.OnTabChangeListener l) 设置TabChanged事件的响应处理
  public void setup () 这个函数后面介绍
 
TabHost.TabSpec
  从上面的函数可以知道如何添加tab了,要注意,这里的Tag(标签),不是Tab按钮上的文字。
  而要设置tab的label和content,需要设置TabHost.TabSpec类。 引用SDK里面的话——“A tab has a tab indicator, content, and a tag that is used to keep track of it.”,TabHost.TabSpec就是管理这3个东西:
  public String getTag ()
  public TabHost.TabSpec setContent
  public TabHost.TabSpec setIndicator
 
  我理解这里的Indicator就是Tab上的label,它可以
  设置label: setIndicator (CharSequence label)
  或者同时设置label和iconsetIndicator (CharSequence label, Drawable icon)
  或者直接指定某个view: setIndicator (View view)
  
  对于Content,就是Tab里面的内容,可以
  设置View的id: setContent(int viewId)
  或者TabHost.TabContentFactory的createTabContent(String tag)来处理:setContent(TabHost.TabContentFactory contentFactory)
  或者用new Intent来引入其他Activity的内容:setContent(Intent intent)

public class TabWidgetActivity extends TabActivity implements
OnTabChangeListener {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);


TabHost TabHost = getTabHost();
TabHost.addTab(TabHost.newTabSpec("选项卡一").setIndicator("TAB 1",
getResources().getDrawable(R.drawable.ic_launcher)).setContent(
R.id.textview1));
TabHost.addTab(TabHost.newTabSpec("选项卡二").setIndicator("TAB 2",
getResources().getDrawable(R.drawable.ic_launcher)).setContent(
R.id.textview2));
TabHost.addTab(TabHost.newTabSpec("选项卡三").setIndicator("TAB 3",
getResources().getDrawable(R.drawable.ic_launcher)).setContent(
R.id.textview2));


TabHost.setCurrentTab(0);
TabHost.setOnTabChangedListener(this);
}


@Override
public void onTabChanged(String tabId) {
Dialog dialog = new AlertDialog.Builder(TabWidgetActivity.this)
.setTitle("提示").setMessage("当前选中:" + tabId + "标签")
.setPositiveButton("确定", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
dialog.cancel();
}
}).create();// 创建按钮


dialog.show();


}
}


<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/tabhost"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <LinearLayout 
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical">
        
        <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">
            <TextView
                android:id="@+id/textview1"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:text="this is a tab" />
            <TextView
                android:id="@+id/textview2"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:text="this is another tab" />
            <TextView
                android:id="@+id/textview3"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:text="this is a third tab" />
        </FrameLayout>
    </LinearLayout>
</TabHost>




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值