[关闭]
@gogogodeng 2017-08-10T13:43:48.000000Z 字数 2635 阅读 410

TabHost

android控件


tabhost就是一个tab选项卡切换控件

布局

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:orientation="vertical"
  4. android:layout_width="match_parent"
  5. android:layout_height="match_parent">
  6. <TabHost
  7. android:layout_width="match_parent"
  8. android:layout_height="wrap_content" android:id="@+id/tabHost" android:layout_gravity="center_horizontal">
  9. <LinearLayout
  10. android:layout_width="match_parent"
  11. android:layout_height="match_parent"
  12. android:orientation="vertical">
  13. <TabWidget
  14. android:id="@android:id/tabs"
  15. android:layout_width="match_parent"
  16. android:layout_height="match_parent">
  17. </TabWidget>
  18. <FrameLayout
  19. android:id="@android:id/tabcontent"
  20. android:layout_width="match_parent"
  21. android:layout_height="match_parent">
  22. <LinearLayout
  23. android:id="@+id/tab1"
  24. android:layout_width="match_parent"
  25. android:layout_height="wrap_content"
  26. android:orientation="vertical">
  27. <ImageView
  28. android:layout_width="wrap_content"
  29. android:layout_height="wrap_content"
  30. android:id="@+id/imageView"
  31. android:src="@drawable/loll01"
  32. android:layout_gravity="center"/>
  33. </LinearLayout>
  34. <LinearLayout
  35. android:id="@+id/tab2"
  36. android:layout_width="match_parent"
  37. android:layout_height="wrap_content"
  38. android:orientation="vertical">
  39. <ImageView
  40. android:layout_width="wrap_content"
  41. android:layout_height="wrap_content"
  42. android:id="@+id/imageView10"
  43. android:src="@drawable/loll02"
  44. android:layout_gravity="center"/>
  45. </LinearLayout>
  46. <LinearLayout
  47. android:id="@+id/tab3"
  48. android:layout_width="match_parent"
  49. android:layout_height="wrap_content"
  50. android:orientation="vertical">
  51. <ImageView
  52. android:layout_width="wrap_content"
  53. android:layout_height="wrap_content"
  54. android:id="@+id/imageView11"
  55. android:src="@drawable/loll03"
  56. android:layout_gravity="center"/>
  57. </LinearLayout>
  58. </FrameLayout>
  59. </LinearLayout>
  60. </TabHost>
  61. </LinearLayout>

其中tabs和tabcontent这2个id不能更改

java代码

  1. package com.testapp.app;
  2. import android.app.Activity;
  3. import android.os.Bundle;
  4. import android.widget.TabHost;
  5. /**
  6. * Created by develop on 2015/11/17.
  7. */
  8. public class TabHostActivity extends Activity {
  9. private TabHost tabHost;
  10. @Override
  11. protected void onCreate(Bundle savedInstanceState) {
  12. super.onCreate(savedInstanceState);
  13. setContentView(R.layout.tabhost);
  14. tabHost = (TabHost) this.findViewById(R.id.tabHost);
  15. tabHost.setup();
  16. tabHost.addTab(tabHost.newTabSpec("tab_1")
  17. .setContent(R.id.tab1)
  18. .setIndicator("TAB1", null));
  19. tabHost.addTab(tabHost.newTabSpec("tab_2")
  20. .setContent(R.id.tab2)
  21. .setIndicator("TAB2", null));
  22. tabHost.addTab(tabHost.newTabSpec("tab_3")
  23. .setContent(R.id.tab3)
  24. .setIndicator("TAB3", null));
  25. tabHost.setCurrentTab(0);
  26. /**
  27. * 选项卡切换事件
  28. */
  29. tabHost.setOnTabChangedListener(new TabHost.OnTabChangeListener() {
  30. @Override
  31. public void onTabChanged(String tabId) {
  32. }
  33. });
  34. }
  35. }

效果

效果图

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注