使用TabHost有两种方法,一种是继承TabActivity;一种是不继承TabActivity。

第一种:继承TabActivity

1、如果我们使用extendsTabAcitivty,如同ListActivity,TabHost必须设置为@android :id/tabhost
2、TabWidget必须设置android:id为@android :id/tabs
3、FrameLayout需要设置android:id为@android :id/tabcontent

main.xml示例:

<?xml version="1.0" encoding="UTF-8"?>
<TabHost
android:id="@android :id/tabhost"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<FrameLayout
android:id="@android :id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="0.0dip"
android:layout_weight="1.0" />
<TabWidget android:id="@android:id/tabs"
android:visibility="gone"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="0.0" />
<RadioGroup
android:gravity="center_vertical"
android:layout_gravity="bottom"
android:orientation="horizontal"
android:id="@id/main_radio"
android:background="@drawable/maintab_toolbar_bg"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<RadioButton
android:id="@id/radio_button0"
android:layout_marginTop="2.0dip"
android:text="@string/main_home"
android:drawableTop="@drawable/icon_1_n"
style="@style/main_tab_bottom" />
</RadioGroup>
</LinearLayout>
</TabHost>

TabActivity 相关函数如下:

public TabWidget getTabWidget () 获得当前TabActivity 的TabWidget

public TabHost getTabHost () 获得当前TabActivity的TabHost

public void setDefaultTab (String tag) 这两个函数很易懂, 就是设置默认的Tab public void setDefaultTab (int index) 通过tab名——tag或者index(从0开始) protected void onRestoreInstanceState (Bundle state) 这 两个函数的介绍可以 protected void onSaveInstanceState (Bundle outState) 参考 Activity的生命周期

TabHost:

现在看看TabHost类,它有3个内嵌类:1个类TabHost.TabSpec,2个接口 TabHost.TabContentFactory和TabHost.OnTabChangeListener。

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,它可以 设置labelsetIndicator (CharSequence label) 或者同时 设置label和iconsetIndicator (CharSequence label, Drawable icon) 或者直接 指定某个viewsetIndicator (View view) 对于 Content ,就是Tab里面的内容,可以 设置View的idsetContent(int viewId) 或者 TabHost.TabContentFactory 的createTabContent(String tag)来处理: setContent(TabHost.TabContentFactory contentFactory) 或者用 new Intent 来引入其他Activity的内容: setContent(Intent intent) Acitvit里面的代码代码

package com.yang.tabletest;

import android.app.TabActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.widget.TabHost;

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

//获得当前TabActivity的TabHost
TabHost tabHost = getTabHost();

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

tabHost.addTab(tabHost.newTabSpec("tab1")
.setIndicator("主页")
.setContent(R.id.view1));


tabHost.addTab(tabHost.newTabSpec("tab2")
.setIndicator("标题")
.setContent(R.id.view2));
tabHost.addTab(tabHost.newTabSpec("tab3")
.setIndicator("简介")
.setContent(R.id.view3));
tabHost.addTab(tabHost.newTabSpec("tab4")
.setIndicator("关于")
.setContent(R.id.view4));
}


}

tabls.xml里面的代码

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

<TextView android:id="@+id/view1"
android:background="@drawable/blue"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="@string/tabs_1_tab_1"/>

<TextView android:id="@+id/view2"
android:background="@drawable/red"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="@string/tabs_1_tab_2"/>

<TextView android:id="@+id/view3"
android:background="@drawable/green"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="@string/tabs_1_tab_3"/>

<TextView android:id="@+id/view4"
android:background="@drawable/green"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="@string/tabs_1_tab_4"/>

</FrameLayout>

string.xml的代码


<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">Hello World, TableTestAcitivity!</string>
<string name="app_name">阿福学习</string>
<string name="tabs_1_tab_1">主页</string>
<string name="tabs_1_tab_2">标题</string>
<string name="tabs_1_tab_3">关于</string>
<string name="tabs_1_tab_4">返回</string>
</resources>

color.xml代码

<?xml version="1.0" encoding="utf-8"?>
<resources>
<drawable name="darkgray">#404040ff</drawable>

<drawable name="red">#ff00ff</drawable>
<drawable name="green">#0ff0ff</drawable>
<drawable name="lightgray">#c0c0c0ff</drawable>

<drawable name="yellow">#ffFF33ff</drawable>
<drawable name="blue">#00ffff</drawable>
<drawable name="gray">#808080ff</drawable>
<drawable name="magenta">#ff6699ff</drawable>
<drawable name="cyan">#66ffffff</drawable>
<drawable name="black">#000000</drawable>
<drawable name="white">#FFFFFF</drawable>
</resources>

第二个例子代码:

package com.yang.tabletest;

import android.app.TabActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TabHost;
import android.widget.TextView;

public class TableTestAcitivity extends TabActivity implements TabHost.TabContentFactory{
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

final TabHost tabHost = getTabHost();
tabHost.addTab(tabHost.newTabSpec("tab1")
.setIndicator("主页", getResources().getDrawable(R.drawable.test))
.setContent(this));
tabHost.addTab(tabHost.newTabSpec("tab2")
.setIndicator("标题",getResources().getDrawable(R.drawable.test))
.setContent(this));
tabHost.addTab(tabHost.newTabSpec("tab3")
.setIndicator("关于",getResources().getDrawable(R.drawable.test))
.setContent(this));
}

@Override
public View createTabContent(String arg0) {
final TextView tv = new TextView(this);
tv.setText("Content for tab with tag " + arg0);

return tv;
}
}

原文:http://yangguangfu.iteye.com/blog/679001

更多相关文章

  1. android事件处理
  2. android 布局
  3. Android中使用WebView, WebChromeClient和WebViewClient加载网页
  4. Android监听软键盘的方式
  5. Widget-进度条
  6. android5.0以上对于APP_SWITCH和HOME键的处理
  7. android中 代码实现截图功能(静态+动态视频)
  8. Android(安卓)开机启动过程
  9. 箭头函数的基础使用

随机推荐

  1. Google Android(安卓)SDK开发范例大全 下
  2. android:绘图 (android.graphics包)
  3. Google Cloud Messaging for Android(安
  4. android NDk环境编译-----原创总结
  5. Android应用程序的快速启动是怎样炼成的
  6. Android的内存泄漏和调试
  7. Android初中级阶段――一个新的开始,新的
  8. TabHost和android:layout_height="0.0dip
  9. Android(安卓)轻松实现语音朗读
  10. Android(安卓)Debug Bridge (adb)