打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
TabHost两种实现方式

第一种:继承TabActivity,从TabActivity中用getTabHost()方法获取TabHost。只要定义具体Tab内容布局就行了.

xml布局:

  1. <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. android:layout_width="fill_parent" android:layout_height="fill_parent">
  3. <LinearLayout android:id="@+id/widget_layout_Blue"
  4. android:layout_width="fill_parent" android:layout_height="fill_parent"
  5. android:orientation="vertical" >
  6. <EditText android:id="@+id/widget34" android:layout_width="fill_parent"
  7. android:layout_height="wrap_content" android:text="EditText"
  8. android:textSize="18sp">
  9. </EditText>
  10. <Button android:id="@+id/widget30" android:layout_width="wrap_content"
  11. android:layout_height="wrap_content" android:text="Button">
  12. </Button>
  13. </LinearLayout>
  14. <LinearLayout android:id="@+id/widget_layout_red"
  15. android:layout_width="fill_parent" android:layout_height="fill_parent"
  16. android:orientation="vertical" >
  17. <AnalogClock android:id="@+id/widget36"
  18. android:layout_width="wrap_content" android:layout_height="wrap_content">
  19. </AnalogClock>
  20. </LinearLayout>
  21. <LinearLayout android:id="@+id/widget_layout_green"
  22. android:layout_width="fill_parent" android:layout_height="fill_parent"
  23. android:orientation="vertical">
  24. <RadioGroup android:id="@+id/widget43"
  25. android:layout_width="166px" android:layout_height="98px"
  26. android:orientation="vertical">
  27. <RadioButton android:id="@+id/widget44"
  28. android:layout_width="wrap_content" android:layout_height="wrap_content"
  29. android:text="RadioButton">
  30. </RadioButton>
  31. <RadioButton android:id="@+id/widget45"
  32. android:layout_width="wrap_content" android:layout_height="wrap_content"
  33. android:text="RadioButton">
  34. </RadioButton>
  35. </RadioGroup>
  36. </LinearLayout>
  37. </FrameLayout>
  38. java代码:
  39. super.onCreate(savedInstanceState);
  40. myTabhost=this.getTabHost();
  41. //get Tabhost
  42. LayoutInflater.from(this).inflate(R.layout.main, myTabhost.getTabContentView(), true);
  43. myTabhost.setBackgroundColor(Color.argb(150, 22, 70, 150));
  44. myTabhost
  45. .addTab(myTabhost.newTabSpec("One")// make a new Tab
  46. .setIndicator("A")
  47. // set the Title and Icon
  48. .setContent(R.id.widget_layout_Blue));
  49. // set the layout
  50. myTabhost
  51. .addTab(myTabhost.newTabSpec("Two")// make a new Tab
  52. .setIndicator("B",
  53. getResources().getDrawable(R.drawable.mumule))
  54. // set the Title and Icon
  55. .setContent(R.id.widget_layout_green));
  56. // set the layout
  57. myTabhost
  58. .addTab(myTabhost.newTabSpec("Three")// make a new Tab
  59. .setIndicator("C",
  60. getResources().getDrawable(R.drawable.notepad))
  61. // set the Title and Icon
  62. .setContent(R.id.widget_layout_red));

第二种:不用继承TabActivity,在布局文件中定义TabHost即可,但是TabWidget的id必须是
@android:id/tabs,FrameLayout的id必须是@android:id/tabcontent。TabHost的id可以自定义.

xml布局:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:id="@+id/hometabs"
  4. android:orientation="vertical"
  5. android:layout_width="fill_parent"
  6. android:layout_height="fill_parent">
  7. <TabHost android:id="@+id/tabhost"
  8. android:layout_width="wrap_content"
  9. android:layout_height="wrap_content">
  10. <LinearLayout
  11. android:orientation="vertical"
  12. android:layout_width="fill_parent"
  13. android:layout_height="fill_parent">
  14. <TabWidget android:id="@android:id/tabs"
  15. android:orientation="horizontal"
  16. android:layout_width="wrap_content"
  17. android:layout_height="wrap_content">
  18. </TabWidget>
  19. <FrameLayout android:id="@android:id/tabcontent"
  20. android:layout_width="wrap_content"
  21. android:layout_height="wrap_content">
  22. <TextView android:id="@+id/view1"
  23. android:layout_width="fill_parent"
  24. android:layout_height="fill_parent"/>
  25. <TextView android:id="@+id/view2"
  26. android:layout_width="fill_parent"
  27. android:layout_height="fill_parent"/>
  28. <TextView android:id="@+id/view3"
  29. android:layout_width="fill_parent"
  30. android:layout_height="fill_parent"/>
  31. </FrameLayout>
  32. </LinearLayout>
  33. </TabHost>
  34. </LinearLayout>
  35. java代码:
  36. protected void onCreate(Bundle savedInstanceState) {
  37. super.onCreate(savedInstanceState);
  38. setContentView(R.layout.hometabs);
  39. TabHost tabHost = (TabHost) findViewById(R.id.tabhost);
  40. tabHost.setup();
  41. TabWidget tabWidget = tabHost.getTabWidget();
  42. tabHost.addTab(tabHost.newTabSpec("tab1")
  43. .setIndicator("tab1", getResources().getDrawable(R.drawable.mumule))
  44. .setContent(R.id.view1));
  45. tabHost.addTab(tabHost.newTabSpec("tab3")
  46. .setIndicator("tab3")
  47. .setContent(R.id.view3));
  48. tabHost.addTab(tabHost.newTabSpec("tab2")
  49. .setIndicator("tab2")
  50. .setContent(R.id.view2));
  51. final int tabs = tabWidget.getChildCount();
  52. Log.i(TAG, "***tabWidget.getChildCount() : " + tabs);
  53. final int tabWidth = 90;
  54. final int tabHeight = 45;
  55. for (int i = 0; i < tabs; i++) {
  56. }
  57. }
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
TabActivity的美化
史上最全的Android的Tab与TabHost讲解
使用TabWidget实现Tab的切换
防止UI界面被输入法遮挡(画面随输入法自适应)
Android基础概念
Android中文API(114)——TabWidget
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服