2010 - 06 - 07

Android TabWidget/TabHost的使用

文章分类:Java编程 Android TabWidget/TabHost有两种使用方法:

第一种:使用系统自带写好的TabHost(及继承自TabActivity类)具体代码如下:

Java代码 ⃕
  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <FrameLayoutxmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="fill_parent"
  4. android:layout_height="fill_parent">
  5. <LinearLayoutandroid:id="@+id/tab1"
  6. android:layout_width="fill_parent"android:layout_height="fill_parent"
  7. androidrientation="vertical">
  8. <TextViewandroid:id="@+id/TextView1"
  9. android:text="Thisisatab1"android:layout_width="fill_parent"
  10. android:layout_height="wrap_content">
  11. </TextView>
  12. </LinearLayout>
  13. <LinearLayoutandroid:id="@+id/tab2"
  14. android:layout_width="fill_parent"android:layout_height="fill_parent"
  15. androidrientation="vertical">
  16. <TextViewandroid:id="@+id/TextView2"
  17. android:text="Thisisatab2"android:layout_width="fill_parent"
  18. android:layout_height="wrap_content">
  19. </TextView>
  20. </LinearLayout>
  21. <LinearLayoutandroid:id="@+id/tab3"
  22. android:layout_width="fill_parent"android:layout_height="fill_parent"
  23. androidrientation="vertical">
  24. <TextViewandroid:id="@+id/TextView3"
  25. android:text="Thisisatab3"android:layout_width="fill_parent"
  26. android:layout_height="wrap_content">
  27. </TextView>
  28. </LinearLayout>
  29. </FrameLayout>


Java代码 ⃕
  1. packagecom.Aina.Android;
  2. importandroid.app.AlertDialog;
  3. importandroid.app.Dialog;
  4. importandroid.app.TabActivity;
  5. importandroid.content.DialogInterface;
  6. importandroid.os.Bundle;
  7. importandroid.view.LayoutInflater;
  8. importandroid.widget.TabHost;
  9. publicclassTest_TabWidgetextendsTabActivity{
  10. /**Calledwhentheactivityisfirstcreated.*/
  11. privateTabHosttabHost;
  12. @Override
  13. publicvoidonCreate(BundlesavedInstanceState){
  14. super.onCreate(savedInstanceState);
  15. //setContentView(R.layout.main);
  16. tabHost=this.getTabHost();
  17. LayoutInflaterli=LayoutInflater.from(this);
  18. li.inflate(R.layout.main,tabHost.getTabContentView(),true);
  19. tabHost.addTab(tabHost.newTabSpec("Tab_1").setContent(R.id.tab1)
  20. .setIndicator("TAB1",
  21. this.getResources().getDrawable(R.drawable.img1)));
  22. tabHost.addTab(tabHost.newTabSpec("Tab_2").setContent(R.id.tab2)
  23. .setIndicator("TAB2",
  24. this.getResources().getDrawable(R.drawable.img2)));
  25. tabHost.addTab(tabHost.newTabSpec("Tab_3").setContent(R.id.tab3)
  26. .setIndicator("TAB3",
  27. this.getResources().getDrawable(R.drawable.img3)));
  28. tabHost.setCurrentTab(1);
  29. //tabHost.setBackgroundColor(Color.GRAY);
  30. tabHost.setOnTabChangedListener(newTabHost.OnTabChangeListener(){
  31. publicvoidonTabChanged(StringtabId){
  32. Dialogdialog=newAlertDialog.Builder(Test_TabWidget.this)
  33. .setTitle("提示").setMessage(
  34. "选中了"+tabId+"选项卡").setIcon(R.drawable.icon).setPositiveButton("确定",newDialogInterface.OnClickListener(){
  35. publicvoidonClick(DialogInterfacedialog,
  36. intwhich){
  37. //TODOAuto-generatedmethodstub
  38. }
  39. }).create();
  40. dialog.show();
  41. }
  42. });
  43. }
  44. }


第二种:就是定义我们自己的tabHost:不用继承TabActivity,具体代码如下:

Java代码 ⃕
  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <TabHostxmlns:android="http://schemas.android.com/apk/res/android"
  3. android:id="@+id/TabHost01"android:layout_width="fill_parent"
  4. android:layout_height="fill_parent">
  5. <LinearLayoutandroid:layout_width="fill_parent"
  6. android:orientation="vertical"android:layout_height="fill_parent">
  7. <TabWidgetandroid:id="@android:id/tabs"
  8. android:layout_width="fill_parent"
  9. android:layout_height="wrap_content"/>
  10. <FrameLayoutandroid:id="@android:id/tabcontent"
  11. android:layout_width="fill_parent"
  12. android:layout_height="fill_parent">
  13. <LinearLayoutandroid:id="@+id/LinearLayout1"
  14. android:layout_width="fill_parent"
  15. android:layout_height="wrap_content">
  16. <TextViewandroid:text="one"
  17. android:id="@+id/TextView01"android:layout_width="wrap_content"
  18. android:layout_height="wrap_content">
  19. </TextView>
  20. </LinearLayout>
  21. <LinearLayoutandroid:id="@+id/LinearLayout2"
  22. android:layout_width="wrap_content"
  23. android:layout_height="wrap_content">
  24. <TextViewandroid:text="two"
  25. android:id="@+id/TextView02"android:layout_width="fill_parent"
  26. android:layout_height="wrap_content">
  27. </TextView>
  28. </LinearLayout>
  29. <LinearLayoutandroid:id="@+id/LinearLayout3"
  30. android:layout_width="wrap_content"
  31. android:layout_height="wrap_content">
  32. <TextViewandroid:text="three"
  33. android:id="@+id/TextView03"android:layout_width="fill_parent"
  34. android:layout_height="wrap_content">
  35. </TextView>
  36. </LinearLayout>
  37. </FrameLayout>
  38. </LinearLayout>
  39. </TabHost>


Java代码 ⃕
  1. packagecom.Aina.Android;
  2. importandroid.app.Activity;
  3. importandroid.os.Bundle;
  4. importandroid.util.Log;
  5. importandroid.view.LayoutInflater;
  6. importandroid.widget.TabHost;
  7. publicclassTest_TabHostextendsActivity{
  8. /**Calledwhentheactivityisfirstcreated.*/
  9. privateTabHosttabHost;
  10. @Override
  11. publicvoidonCreate(BundlesavedInstanceState){
  12. super.onCreate(savedInstanceState);
  13. setContentView(R.layout.main);
  14. try{
  15. tabHost=(TabHost)this.findViewById(R.id.TabHost01);
  16. tabHost.setup();
  17. tabHost.addTab(tabHost.newTabSpec("tab_1")
  18. .setContent(R.id.LinearLayout1)
  19. .setIndicator("TAB1",this.getResources().getDrawable(R.drawable.img1)));
  20. tabHost.addTab(tabHost.newTabSpec("tab_2")
  21. .setContent(R.id.LinearLayout2).setIndicator("TAB2",
  22. this.getResources().getDrawable(R.drawable.img2)));
  23. tabHost.addTab(tabHost.newTabSpec("tab_3")
  24. .setContent(R.id.LinearLayout3).setIndicator("TAB3",
  25. this.getResources().getDrawable(R.drawable.img3)));
  26. tabHost.setCurrentTab(1);
  27. }catch(Exceptionex){
  28. ex.printStackTrace();
  29. Log.d("EXCEPTION",ex.getMessage());
  30. }
  31. }
  32. }


注意:第二种方法时布局文件中的TabWidget的id必须定义 为:android:id="@android:id/tabs",FrameLayout的id必须定义 为:android:id="@android:id/tabcontent" 其它控件没有限制,否则报错。

更多相关文章

  1. android中自定义Theme以及TitleBar
  2. drawable中的layer-list使用
  3. Android(安卓)近百个项目的源代码,覆盖Android开发的每个领域
  4. Android里把Dialog设置为全屏的方法
  5. android 显示特殊符号
  6. android 中调用接口发送短信
  7. 【Android代码片段之四】设置全屏
  8. android 中调用接口发送短信
  9. Android(安卓)MVVM结合DataBinding的简单实用(Android(安卓)studi

随机推荐

  1. android HAL介绍
  2. Android中的坐标系以及获取坐标的方法
  3. Android(安卓)各种实现Tab效果的实现方式
  4. Android(安卓)内核简单分析
  5. Android(安卓)开发笔记 —— AndroidStud
  6. Android层次化安全架构及核心组件概览
  7. 应用界面主题Theme使用方法
  8. Android系统移植与调试之------->如何修
  9. android的一些开源项目
  10. Android(安卓)轻松实现语音识别