第一种:继承TabActivity,从TabActivity中用getTabHost()方法获取TabHost。只要定义具体Tab内容布局就行了.
<?xml version="1.0" encoding="utf-8"?> <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/FrameLayout01" android:layout_width="wrap_content" android:layout_height="wrap_content"> <TextView android:id="@+id/TextView01" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="所有通话记录"></TextView> <TextView android:id="@+id/TextView02" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="已接来电"></TextView> <TextView android:id="@+id/TextView03" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="未接来电"></TextView> </FrameLayout>
package com.example.testtabhost; import android.app.TabActivity; import android.os.Bundle; import android.view.LayoutInflater; import android.widget.TabHost; import android.widget.Toast; import android.widget.TabHost.OnTabChangeListener; public class MainActivity extends TabActivity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); TabHost th = getTabHost(); //声明TabHost,然后用LayoutInflater过滤出布局来,给TabHost加上含有Tab页面的FrameLayout //from(this)从这个TabActivity获取LayoutInflater //R.layout.main 存放Tab布局 //通过TabHost获得存放Tab标签页内容的FrameLayout //是否将inflate 拴系到根布局元素上 LayoutInflater.from(this).inflate(R.layout.activity_main, th.getTabContentView(), true); //通过TabHost获得存放Tab标签页内容的FrameLayout, //newTabSpecd的作用是获取一个新的 TabHost.TabSpec,并关联到当前 TabHost //setIndicator的作用是指定标签和图标作为选项卡的指示符. //setContent的作用是指定用于显示选项卡内容的视图 ID. th.addTab(th.newTabSpec("all").setIndicator("所有通话记录", getResources().getDrawable(R.drawable.ic_launcher)).setContent(R.id.TextView01)); th.addTab(th.newTabSpec("ok").setIndicator("已接来电",getResources().getDrawable(R.drawable.ic_launcher)).setContent(R.id.TextView02)); th.addTab(th.newTabSpec("cancel").setIndicator("未接来电",getResources().getDrawable(R.drawable.ic_launcher)).setContent(R.id.TextView03)); //setOnTabChangeListener的作业是注册一个回调函数,当任何一个选项卡的选中状态发生改变时调用. th.setOnTabChangedListener( new OnTabChangeListener() { @Override public void onTabChanged(String tabId) { Toast.makeText(MainActivity.this, tabId, Toast.LENGTH_LONG).show(); } } ); } }
第二种:不用继承TabActivity,在布局文件中定义TabHost即可,但是TabWidget的id必须是
@android:id/tabs,FrameLayout的id必须是@android:id/tabcontent。TabHost的id可以自定义.
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/hometabs" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <TabHost android:id="@+id/tabhost" android:layout_width="wrap_content" android:layout_height="wrap_content"> <LinearLayout android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <TabWidget android:id="@android:id/tabs" android:orientation="horizontal" android:layout_width="wrap_content" android:layout_height="wrap_content"> </TabWidget> <FrameLayout android:id="@android:id/tabcontent" android:layout_width="wrap_content" android:layout_height="wrap_content"> <TextView android:id="@+id/view1" android:layout_width="fill_parent" android:layout_height="fill_parent"/> <TextView android:id="@+id/view2" android:layout_width="fill_parent" android:layout_height="fill_parent"/> <TextView android:id="@+id/view3" android:layout_width="fill_parent" android:layout_height="fill_parent"/> </FrameLayout> </LinearLayout> </TabHost> </LinearLayout>
package com.example.testtabhost2; import android.app.Activity; import android.os.Bundle; import android.util.Log; import android.widget.TabHost; import android.widget.TabWidget; public class MainActivity extends Activity { private static final String TAG = "MainActivity"; protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); TabHost tabHost = (TabHost) findViewById(R.id.tabhost); tabHost.setup(); TabWidget tabWidget = tabHost.getTabWidget(); tabHost.addTab(tabHost.newTabSpec("tab1") .setIndicator("tab1", getResources().getDrawable(R.drawable.ic_launcher)) .setContent(R.id.view1)); tabHost.addTab(tabHost.newTabSpec("tab3") .setIndicator("tab3") .setContent(R.id.view3)); tabHost.addTab(tabHost.newTabSpec("tab2") .setIndicator("tab2") .setContent(R.id.view2)); final int tabs = tabWidget.getChildCount(); Log.i(TAG, "***tabWidget.getChildCount() : " + tabs); final int tabWidth = 90; final int tabHeight = 45; for (int i = 0; i < tabs; i++) { /* final View view = tabWidget.getChildAt(i); view.getLayoutParams().width = tabWidth; view.getLayoutParams().height = tabHeight; final TextView tv = (TextView) view.findViewById(android.R.id.title); tv.setTextColor(this.getResources().getColorStateList(android.R.color.black)); MarginLayoutParams tvMLP = (MarginLayoutParams)tv.getLayoutParams(); tvMLP.bottomMargin = 8;*/ } } }