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标签就是 每一个选项卡 和指示器的标识 都是一一对应的。
设置标签选项卡有三项方法
设置标签内容也有三项方法
setContent(int viewId)Specify the id of the view that should be used as the content of the tab. |
|
setContent(Intent intent)Specify an intent to use to launch an activity as the tab content. |
|
setContent(TabHost.TabContentFactory contentFactory)Specify a TabHost.TabContentFactory to use to create the content of the tab. |
|
setIndicator(CharSequence label)Specify a label as the tab indicator. |
|
setIndicator(View view)Specify a view as the tab indicator. |
|
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>
|
原文:http://blog.csdn.net/gaoanchen/article/details/22606397