TabHost探究

本文详细介绍了如何使用TabHost创建Android应用中的选项卡布局,包括获取TabHost对象、添加标签、设置标签选项和内容的方法。通过示例代码展示了如何在布局中添加多个选项卡,并为每个选项卡配置不同的指示器和内容。



TabHost 首先使用

首先使用getTabHost()获取 TabHost对象

 

通过

void

addTab(TabHost.TabSpec tabSpec)添加标签

 

Tab里面的每一个选项卡 和选项卡下面的内容都是由TabSpec来设置的

TabSpec设置

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

一个tab拥有一个指示器 也就是tab选项卡

和一个内容也就是选项卡所对应的帧布局

Spec 是Tabhost的内部类 使用 tabHost.newTabSpec(String tag) 来获取

这个必须知道tag标签

Tab标签就是 每一个选项卡 和指示器的标识 都是一一对应的。

 

设置标签选项卡有三项方法

设置标签内容也有三项方法

TabHost.TabSpec

setContent(int viewId)Specify the id of the view that should be used as the content of the tab.

TabHost.TabSpec

setContent(Intent intent)Specify an intent to use to launch an activity as the tab content.

TabHost.TabSpec

setContent(TabHost.TabContentFactory contentFactory)Specify a TabHost.TabContentFactory to use to create the content of the tab.

TabHost.TabSpec

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

TabHost.TabSpec

setIndicator(View view)Specify a view as the tab indicator.

TabHost.TabSpec

setIndicator(CharSequence label, Drawable icon)Specify a label and icon as the tab indicator.

测试代码

package com.example.android_tabhost2;

 

import android.os.Bundle;

import android.app.Activity;

import android.app.TabActivity;

import android.view.LayoutInflater;

import android.view.Menu;

import android.widget.TabHost;

 

public class MainActivity extends TabActivity {

private TabHost tabHost;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

tabHost = getTabHost();

//使用TabHost 布局

LayoutInflater.from(this).inflate(R.layout.activity_main, tabHost.getTabContentView(),true);

tabHost.addTab(tabHost.newTabSpec("xitongcaidan").

setIndicator("tab1",getResources().getDrawable(R.drawable.ic_launcher) )

.setContent(R.id.tab01));

tabHost.addTab(tabHost.newTabSpec("xitongcaidan2").

setIndicator("tab2",getResources().getDrawable(R.drawable.ic_launcher) )

.setContent(R.id.tab02));

tabHost.addTab(tabHost.newTabSpec("xitongcaidan3").

setIndicator("tab3",getResources().getDrawable(R.drawable.ic_launcher) )

.setContent(R.id.tab03));

//setContentView(R.layout.activity_main);

}

 

@Override

public boolean onCreateOptionsMenu(Menu menu) {

// Inflate the menu; this adds items to the action bar if it is present.

getMenuInflater().inflate(R.menu.main, menu);

return true;

}

 

}

 

Main_activity.xml 代码

<TabHost xmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:tools="http://schemas.android.com/tools"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    >

<LinearLayout

    android:id="@+id/tab01"

    android:orientation="vertical"

    android:layout_width="match_parent"

        android:layout_height="match_parent"

    >

     <TextView

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:text="tabhost1" />

      <TextView

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:text="tabhost1" />

    

</LinearLayout>

<LinearLayout

    android:id="@+id/tab02"

    android:orientation="vertical"

    android:layout_width="match_parent"

        android:layout_height="match_parent"

    >

     <TextView

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:text="tabhost2" />

      <TextView

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:text="tabhost2" />

    

</LinearLayout>

<LinearLayout

    android:id="@+id/tab03"

    android:orientation="vertical"

    android:layout_width="match_parent"

        android:layout_height="match_parent"

    >

     <TextView

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:text="tabhost3" />

      <TextView

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:text="tabhost3" />

    

</LinearLayout>

  

 

</TabHost>

 


 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值