TabWidget/TabHost有两种使用方法: 第一种:使用系统自带写好的TabHost(及继承自TabActivity类)具体代码如下: - <?xml version="1.0" encoding="utf-8"?>
- <FrameLayout xmlns:="http://schemas.android.com/apk/res/android"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent">
- <LinearLayout android:id="@+id/tab1"
- android:layout_width="fill_parent" android:layout_height="fill_parent"
- androidrientation="vertical">
- <TextView android:id="@+id/TextView1"
- android:text="This is a tab1" android:layout_width="fill_parent"
- android:layout_height="wrap_content">
- </TextView>
- </LinearLayout>
- <LinearLayout android:id="@+id/tab2"
- android:layout_width="fill_parent" android:layout_height="fill_parent"
- androidrientation="vertical">
- <TextView android:id="@+id/TextView2"
- android:text="This is a tab2" android:layout_width="fill_parent"
- android:layout_height="wrap_content">
- </TextView>
- </LinearLayout>
- <LinearLayout android:id="@+id/tab3"
- android:layout_width="fill_parent" android:layout_height="fill_parent"
- androidrientation="vertical">
- <TextView android:id="@+id/TextView3"
- android:text="This is a tab3" android:layout_width="fill_parent"
- android:layout_height="wrap_content">
- </TextView>
- </LinearLayout>
- </FrameLayout>
-
复制代码 - package com.Aina.Android;
-
- import android.app.AlertDialog;
- import android.app.Dialog;
- import android.app.TabActivity;
- import android.content.DialogInterface;
- import android.os.Bundle;
- import android.view.LayoutInflater;
- import android.widget.TabHost;
-
- public class Test_TabWidget extends TabActivity {
- /** Called when the activity is first created. */
- private TabHost tabHost;
-
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- // setContentView(R.layout.main);
- tabHost = this.getTabHost();
- LayoutInflater li = LayoutInflater.from(this);
- li.inflate(R.layout.main, tabHost.getTabContentView(), true);
- tabHost.addTab(tabHost.newTabSpec("Tab_1").setContent(R.id.tab1)
- .setIndicator("TAB1",
- this.getResources().getDrawable(R.drawable.img1)));
- tabHost.addTab(tabHost.newTabSpec("Tab_2").setContent(R.id.tab2)
- .setIndicator("TAB2",
- this.getResources().getDrawable(R.drawable.img2)));
- tabHost.addTab(tabHost.newTabSpec("Tab_3").setContent(R.id.tab3)
- .setIndicator("TAB3",
- this.getResources().getDrawable(R.drawable.img3)));
- tabHost.setCurrentTab(1);
- // tabHost.setBackgroundColor(Color.GRAY);
- tabHost.setOnTabChangedListener(new TabHost.OnTabChangeListener() {
-
- public void onTabChanged(String tabId) {
- Dialog dialog = new AlertDialog.Builder(Test_TabWidget.this)
- .setTitle("提示").setMessage(
- "选中了" + tabId + "选项卡").setIcon(R.drawable.icon).setPositiveButton("确定", new DialogInterface.OnClickListener(){
-
- public void onClick(DialogInterface dialog,
- int which) {
- // TODO Auto-generated method stub
-
- }
-
- }).create();
- dialog.show();
-
- }
-
- });
- }
- }
-
复制代码 第二种:就是定义我们自己的tabHost:不用继承TabActivity,具体代码如下: - <?xml version="1.0" encoding="utf-8"?>
- <TabHost xmlns:android="http://schemas.android.com/apk/res/android"
- android:id="@+id/TabHost01" android:layout_width="fill_parent"
- android:layout_height="fill_parent">
- <LinearLayout android:layout_width="fill_parent"
- android:orientation="vertical" android:layout_height="fill_parent">
- <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">
- <LinearLayout android:id="@+id/LinearLayout1"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content">
- <TextView android:text="one"
- android:id="@+id/TextView01" android:layout_width="wrap_content"
- android:layout_height="wrap_content">
- </TextView>
- </LinearLayout>
- <LinearLayout android:id="@+id/LinearLayout2"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content">
- <TextView android:text="two"
- android:id="@+id/TextView02" android:layout_width="fill_parent"
- android:layout_height="wrap_content">
- </TextView>
- </LinearLayout>
- <LinearLayout android:id="@+id/LinearLayout3"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content">
- <TextView android:text="three"
- android:id="@+id/TextView03" android:layout_width="fill_parent"
- android:layout_height="wrap_content">
- </TextView>
- </LinearLayout>
- </FrameLayout>
- </LinearLayout>
- </TabHost>
-
复制代码 - package com.Aina.Android;
-
- import android.app.Activity;
- import android.os.Bundle;
- import android.util.Log;
- import android.view.LayoutInflater;
- import android.widget.TabHost;
-
- public class Test_TabHost extends Activity {
- /** Called when the activity is first created. */
- private TabHost tabHost;
-
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- try{
- tabHost = (TabHost) this.findViewById(R.id.TabHost01);
- tabHost.setup();
-
- tabHost.addTab(tabHost.newTabSpec("tab_1")
- .setContent(R.id.LinearLayout1)
- .setIndicator("TAB1",this.getResources().getDrawable(R.drawable.img1)));
- tabHost.addTab(tabHost.newTabSpec("tab_2")
- .setContent(R.id.LinearLayout2).setIndicator("TAB2",
- this.getResources().getDrawable(R.drawable.img2)));
- tabHost.addTab(tabHost.newTabSpec("tab_3")
- .setContent(R.id.LinearLayout3).setIndicator("TAB3",
- this.getResources().getDrawable(R.drawable.img3)));
- tabHost.setCurrentTab(1);
- }catch(Exception ex){
- ex.printStackTrace();
- Log.d("EXCEPTION", ex.getMessage());
- }
-
- }
- }
-
复制代码 注意:第二种方法时布局文件中的TabWidget的id必须定义为:android:id="@android:id/tabs",FrameLayout的id必须定义为:android:id="@android:id/tabcontent" 其它控件没有限制,否则报错。 1.当我们继承自一个TabActivity之后,就可以通过getTabHost()方法得到一个TabHost对象,接着再通过该对象得到TabWidget对象。 - final TabHost tabs = getTabHost();
- final TabWidget tabWidget = tabs.getTabWidget();
final TabHost tabs = getTabHost();final TabWidget tabWidget = tabs.getTabWidget(); 2.接下来改写tabWidget中的各个标签对象的属性值,进而实现“定制”的功能,这里用到的就是Java的反射机制。 - for (int i = 0; i < tabWidget.getChildCount(); i++) {
-
-
-
- tabWidget.getChildAt(i).getLayoutParams().height = height;
- tabWidget.getChildAt(i).getLayoutParams().width = width;
-
-
-
-
- final TextView tv = (TextView) tabWidget.getChildAt(i)
- .findViewById(android.R.id.title);
-
- tv.setTextColor(this.getResources().getColorStateList(
- android.R.color.white));
-
-
-
-
-
-
-
-
-
- Log.d("debug", "version is "+Build.VERSION.RELEASE);
-
- final String VERSION = Build.VERSION.RELEASE.substring(0, 3);
- Log.d("debug", "version is "+VERSION);
- if (Float.valueOf(VERSION) <= 2.1) {
- try {
- Class<TabWidget> tabWidgetClass = (Class<TabWidget>) tabWidget.getClass();
- mBottomLeftStrip = tabWidgetClass.getDeclaredField("mBottomLeftStrip");
- mBottomRightStrip = tabWidgetClass.getDeclaredField("mBottomRightStrip");
-
- if (!mBottomLeftStrip.isAccessible()) {
-
- mBottomLeftStrip.setAccessible(true);
- }
- if (!mBottomRightStrip.isAccessible()) {
- mBottomRightStrip.setAccessible(true);
- }
-
- mBottomLeftStrip.set(tabWidget,
- getResources().getDrawable(R.drawable.alpha_00));
- mBottomRightStrip.set(tabWidget, getResources()
- .getDrawable(R.drawable.alpha_00));
-
- } catch (Exception e) {
- e.printStackTrace();
- }
- } else {
-
-
-
- }
- View vvv = tabWidget.getChildAt(i);
- if (tabs.getCurrentTab() == i) {
- vvv.setBackgroundDrawable(getResources().getDrawable(
- R.drawable.tab_enabled));
- } else {
- vvv.setBackgroundDrawable(getResources().getDrawable(
- R.drawable.tab_selected));
- }
-
- }
|