首先先看一个小例子,接着讲授理原






  1. TabTest.java

  2. view plaincopy to clipboardprint?
  3. package org.hualang.tab;
  4. import android.app.Activity;
  5. import android.app.TabActivity;
  6. import android.graphics.Color;
  7. import android.os.Bundle;
  8. import android.widget.TabHost;
  9. import android.widget.Toast;
  10. import android.widget.TabHost.OnTabChangeListener;
  11. public class TabTest extends TabActivity {
  12. /** Called when the activity is first created. */
  13. TabHost tabhost;
  14. @Override
  15. public void onCreate(Bundle savedInstanceState) {
  16. super.onCreate(savedInstanceState);
  17. setContentView(R.layout.main);
  18. //获得TabHost对象
  19. tabhost = getTabHost();
  20. //为TabHost添加签标
  21. //新建一个newTabSpec(newTabSpec)
  22. //设置其签标和标图(setIndicator)
  23. //设置容内(setContent)
  24. tabhost.addTab(tabhost.newTabSpec("tab1")
  25. .setIndicator("TAB 1",getResources().getDrawable(R.drawable.img1))
  26. .setContent(R.id.text1));
  27. tabhost.addTab(tabhost.newTabSpec("tab2")
  28. .setIndicator("TAB 2",getResources().getDrawable(R.drawable.img2))
  29. .setContent(R.id.text2));
  30. tabhost.addTab(tabhost.newTabSpec("tab3")
  31. .setIndicator("TAB 3",getResources().getDrawable(R.drawable.img3))
  32. .setContent(R.id.text3));
  33. //设置TabHost的背景颜色
  34. //tabhost.setBackgroundColor(Color.argb(150,22,70,150));
  35. //设置TabHost的背景图片资源
  36. tabhost.setBackgroundResource(R.drawable.bg0);
  37. //设置前当示显哪个签标
  38. tabhost.setCurrentTab(0);
  39. //签标换切事件处理,setOnTabChangedListener
  40. tabhost.setOnTabChangedListener(new OnTabChangeListener()
  41. {
  42. public void onTabChanged(String tabId)
  43. {
  44. Toast toast=Toast.makeText(getApplicationContext(), "现在是"+tabId+"签标", Toast.LENGTH_SHORT);
  45. toast.show();
  46. }
  47. });

  48. }
  49. }




main.xml




  1. <?xml version="1.0" encoding="utf-8"?>
  2. <TabHost xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:id="@android:id/tabhost"
  4. android:layout_width="fill_parent"
  5. android:layout_height="fill_parent">
  6. <LinearLayout
  7. android:orientation="vertical"
  8. android:layout_width="fill_parent"
  9. android:layout_height="fill_parent">
  10. <TabWidget
  11. android:id="@android:id/tabs"
  12. android:layout_width="fill_parent"
  13. android:layout_height="wrap_content" />
  14. <FrameLayout
  15. android:id="@android:id/tabcontent"
  16. android:layout_width="fill_parent"
  17. android:layout_height="fill_parent">
  18. <TextView
  19. android:id="@+id/text1"
  20. android:layout_width="fill_parent"
  21. android:layout_height="fill_parent"
  22. android:text="选项卡1" />
  23. <TextView
  24. android:id="@+id/text2"
  25. android:layout_width="fill_parent"
  26. android:layout_height="fill_parent"
  27. android:text="选项卡2" />
  28. <TextView
  29. android:id="@+id/text3"
  30. android:layout_width="fill_parent"
  31. android:layout_height="fill_parent"
  32. android:text="选项卡3" />
  33. </FrameLayout>
  34. </LinearLayout>
  35. </TabHost>





Android TabWidget的实现可以分为二种,一种是应用准标TabActivity实现,另外一种可以自定义方法实现,种这方法实现起来对相较比复杂,但对于要实现较比多元化的view是很好的,这里我们简略看下源码


一、通用做法


继承TabActivity,实现自己的TabActivity



  1. import android.app.Activity;
  2. import android.app.TabActivity;
  3. import android.content.Intent;
  4. import android.os.Bundle;
  5. import android.widget.TabHost;
  6. import android.widget.TabHost.OnTabChangeListener;
  7. public class TabWidgetDemo2 extends TabActivity implements OnTabChangeListener {
  8. private TabHost mTabHost;

  9. @Override
  10. protected void onCreate(Bundle savedInstanceState) {
  11. // TODO Auto-generated method stub
  12. super.onCreate(savedInstanceState);

  13. setContentView(R.layout.tabwidgetdemo2);
  14. mTabHost = getTabHost();
  15. mTabHost.setOnTabChangedListener(this);
  16. setupTab1();
  17. setupTab2();
  18. mTabHost.setCurrentTab(1);
  19. }
  20. private void setupTab2() {
  21. // TODO Auto-generated method stub
  22. Intent intent = new Intent();
  23. intent.setAction(Intent.ACTION_MAIN);
  24. intent.setClass(this, TabWidget2.class);
  25. mTabHost.addTab(mTabHost.newTabSpec("TabWidget2")
  26. .setIndicator("TabWidget2",getResources().getDrawable(R.drawable.icon))
  27. .setContent(intent));
  28. }
  29. private void setupTab1() {
  30. // TODO Auto-generated method stub
  31. Intent intent = new Intent();
  32. intent.setAction(Intent.ACTION_MAIN);
  33. intent.setClass(this, TabWidget1.class);
  34. mTabHost.addTab(mTabHost.newTabSpec("TabWidget1")
  35. .setIndicator("TabWidget1",getResources().getDrawable(R.drawable.icon))
  36. .setContent(intent));
  37. }
  38. public void onTabChanged(String tabId) {
  39. // TODO Auto-generated method stub
  40. Activity activity = getLocalActivityManager().getActivity(tabId);
  41. if (activity != null) {
  42. activity.onWindowFocusChanged(true);
  43. }
  44. }


  45. }



二个tab对应的Activity,先看TabWidget1,这个类在第二种实现中还会用到,因此我们可以看到对Action的判断。



  1. import android.app.Activity;
  2. import android.content.Intent;
  3. import android.os.Bundle;
  4. import com.android.exampledemo.R;
  5. import com.android.exampledemo.util.DemoUtils;
  6. public class TabWidget1 extends Activity {
  7. @Override
  8. protected void onCreate(Bundle savedInstanceState) {
  9. // TODO Auto-generated method stub
  10. super.onCreate(savedInstanceState);

  11. Intent intent = this.getIntent();
  12. if (intent.getAction().equals(Intent.ACTION_MAIN)){
  13. setContentView(R.layout.tabwidgetdemo2_1);
  14. }
  15. else {
  16. setContentView(R.layout.tabwidget_1);
  17. DemoUtils.updateButtonBar((Activity)this,R.id.contactstab);
  18. }
  19. }
  20. }





再看一下TabWidget2,这个Activity我们在第二种实现方法中也会用到。


  1. import com.android.exampledemo.R;
  2. import com.android.exampledemo.util.DemoUtils;
  3. import android.app.Activity;
  4. import android.content.Intent;
  5. import android.os.Bundle;
  6. public class TabWidget2 extends Activity {
  7. @Override
  8. protected void onCreate(Bundle savedInstanceState) {
  9. // TODO Auto-generated method stub
  10. super.onCreate(savedInstanceState);

  11. Intent intent = this.getIntent();

  12. if (intent.getAction().equals(Intent.ACTION_MAIN)){
  13. setContentView(R.layout.tabwidgetdemo2_1);
  14. }
  15. else {
  16. setContentView(R.layout.tabwidget_2);
  17. DemoUtils.updateButtonBar((Activity)this,R.id.groupstab);
  18. }
  19. }
  20. }





最后就是各个Activity对应的layout


1.tabwidgetdemo2.xml



  1. <?xml version="1.0" encoding="utf-8"?>
  2. <TabHost
  3. xmlns:android="http://schemas.android.com/apk/res/android"
  4. android:id="@android:id/tabhost"
  5. android:layout_width="fill_parent"
  6. android:layout_height="fill_parent">
  7. <LinearLayout
  8. android:orientation="vertical"
  9. android:layout_width="fill_parent"
  10. android:layout_height="fill_parent">
  11. <TabWidget android:id="@android:id/tabs"
  12. android:layout_width="fill_parent"
  13. android:layout_height="68dip"
  14. android:paddingLeft="1dip"
  15. android:paddingRight="1dip"
  16. android:paddingTop="4dip"
  17. />
  18. <FrameLayout android:id="@android:id/tabcontent"
  19. android:layout_width="fill_parent"
  20. android:layout_height="0dip"
  21. android:layout_weight="1"
  22. />
  23. </LinearLayout>
  24. </TabHost>



2.二个sub tab对应的layout




  1. Layout1
  2. <?xml version="1.0" encoding="utf-8"?>
  3. <LinearLayout
  4. xmlns:android="http://schemas.android.com/apk/res/android"
  5. android:layout_width="fill_parent"
  6. android:layout_height="fill_parent"
  7. android:background="#FFF">
  8. <TextView android:id="@+id/textview"
  9. android:layout_width="wrap_content"
  10. android:layout_height="wrap_content"
  11. android:text="Tab Widget first">
  12. </TextView>
  13. </LinearLayout>
  14. Layout2
  15. <?xml version="1.0" encoding="utf-8"?>
  16. <LinearLayout
  17. xmlns:android="http://schemas.android.com/apk/res/android"
  18. android:layout_width="fill_parent"
  19. android:layout_height="fill_parent"
  20. android:background="#FFF">
  21. <TextView android:id="@+id/textview"
  22. android:layout_width="wrap_content"
  23. android:layout_height="wrap_content"
  24. android:text="Tab Widget second">
  25. </TextView>
  26. </LinearLayout>







方法2:


先创立一个Activity (TabWidgetDemo)



  1. 1.TabWidgetDemo.java
  2. import com.android.exampledemo.R;
  3. import com.android.exampledemo.util.DemoUtils;
  4. import android.app.Activity;
  5. import android.content.Context;
  6. import android.content.SharedPreferences;
  7. import android.os.Bundle;
  8. //not use tabhost to organized
  9. public class TabWidgetDemo extends Activity {
  10. @Override
  11. protected void onCreate(Bundle savedInstanceState) {
  12. // TODO Auto-generated method stub
  13. super.onCreate(savedInstanceState);
  14. //int activeTab = DemoUtils.getIntPref(this, "activetab", R.id.artisttab);
  15. SharedPreferences prefs =
  16. getSharedPreferences(getPackageName(), Context.MODE_PRIVATE);
  17. int activeTab = prefs.getInt("activetab", R.id.contactstab);
  18. if (activeTab != R.id.contactstab
  19. && activeTab != R.id.groupstab) {
  20. activeTab = R.id.contactstab;
  21. }
  22. DemoUtils.activateTab(this, activeTab);
  23. }
  24. }
  25. 2.DemoUtils
  26. import android.app.Activity;
  27. import android.content.Intent;
  28. import android.net.Uri;
  29. import android.view.View;
  30. import android.widget.TabWidget;
  31. import com.android.exampledemo.R;
  32. public class DemoUtils {
  33. static int sActiveTabIndex = -1;

  34. public static void activateTab(Activity a,int active_id){
  35. Intent intent = new Intent(Intent.ACTION_PICK);
  36. switch (active_id) {
  37. case R.id.contactstab:
  38. intent.setDataAndType(Uri.EMPTY, "vnd.android.cursor.dir/tb_contacts");
  39. break;
  40. case R.id.groupstab:
  41. intent.setDataAndType(Uri.EMPTY, "vnd.android.cursor.dir/tb_groups");
  42. break;
  43. default:
  44. return;
  45. }
  46. intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
  47. a.startActivity(intent);
  48. a.finish();
  49. a.overridePendingTransition(0,0);
  50. }


  51. public static void updateButtonBar(Activity a, int highlight) {
  52. final TabWidget ll = (TabWidget) a.findViewById(R.id.buttonbar);

  53. for (int i = ll.getChildCount() - 1; i >= 0; i--) {
  54. View v = ll.getChildAt(i);
  55. boolean isActive = (v.getId() == highlight);
  56. if (isActive) {
  57. ll.setCurrentTab(i);
  58. sActiveTabIndex = i;
  59. }

  60. v.setTag(i);
  61. v.setOnClickListener(new View.OnClickListener() {
  62. public void onClick(View v) {
  63. int id = v.getId();
  64. if (id == ll.getChildAt(sActiveTabIndex).getId()) {
  65. return;
  66. }
  67. activateTab((Activity)ll.getContext(),id );
  68. ll.setCurrentTab((Integer) v.getTag());
  69. }});
  70. }
  71. }
  72. }






二个Tab sub activity前一方法中经已给出,这里我们只需要看一下layout的实现就能够了


1>buttonbar.xml



  1. <?xml version="1.0" encoding="utf-8"?>
  2. <TabWidget xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:id="@+id/buttonbar"
  4. android:layout_width="match_parent"
  5. android:layout_height="wrap_content" >
  6. <TextView
  7. android:id="@+id/contactstab"
  8. android:focusable="true"
  9. android:drawableTop="@drawable/icon"
  10. android:background="@drawable/buttonbarbackground"
  11. android:text="Contacts"
  12. android:textColor="@color/tab_indicator_text"
  13. android:textAppearance="?android:attr/textAppearanceSmall"
  14. android:paddingTop="7dip"
  15. android:paddingBottom="2dip"
  16. android:gravity="center"
  17. android:layout_weight="1"
  18. android:layout_marginLeft="-3dip"
  19. android:layout_marginRight="-3dip"
  20. android:layout_width="match_parent"
  21. android:layout_height="84dip"
  22. android:singleLine="true"
  23. android:ellipsize="marquee" />
  24. <TextView
  25. android:id="@+id/groupstab"
  26. android:focusable="true"
  27. android:drawableTop="@drawable/icon"
  28. android:background="@drawable/buttonbarbackground"
  29. android:text="Group"
  30. android:textColor="@color/tab_indicator_text"
  31. android:textAppearance="?android:attr/textAppearanceSmall"
  32. android:paddingTop="7dip"
  33. android:paddingBottom="2dip"
  34. android:gravity="center"
  35. android:layout_weight="1"
  36. android:layout_marginLeft="-3dip"
  37. android:layout_marginRight="-3dip"
  38. android:layout_width="match_parent"
  39. android:layout_height="84dip"
  40. android:singleLine="true"
  41. android:ellipsize="marquee" />
  42. </TabWidget>



2>tabwidget_1.xml



  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout
  3. xmlns:android="http://schemas.android.com/apk/res/android"
  4. android:layout_width="fill_parent"
  5. android:layout_height="fill_parent">

  6. <include layout="@layout/battonbar" />

  7. <ExpandableListView android:id="@+id/android:list"
  8. android:layout_width="fill_parent"
  9. android:layout_height="wrap_content"
  10. android:footerDividersEnabled="true"
  11. android:fadeScrollbars="true"
  12. android:drawSelectorOnTop="true">
  13. </ExpandableListView>

  14. </LinearLayout>



3> tabwidget_2.xml



  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout
  3. xmlns:android="http://schemas.android.com/apk/res/android"
  4. android:layout_width="fill_parent"
  5. android:layout_height="fill_parent">

  6. <include layout="@layout/battonbar" />

  7. </LinearLayout>