1. 最简单的TabHost,Tab来自于layout下的元素(只从1个Layout中取数据)

(1)效果图

e6c12093-ddd6-376b-a209-2101b9bb5436.jpg

(2)代码

1)tab_demo.xml


[xhtml] view plain copy
  1. <?xmlversion="1.0"encoding="utf-8"?>

  2. <FrameLayoutxmlns:android="http://schemas.android.com/apk/res/android"

  3. android:orientation="vertical"

  4. android:layout_width="fill_parent"

  5. android:layout_height="fill_parent"

  6. >

  7. <TextViewandroid:id="@+id/tab_demo_tv1"

  8. android:layout_width="fill_parent"

  9. android:layout_height="fill_parent"

  10. android:text="tab_demo_tv1"

  11. />

  12. <TextViewandroid:id="@+id/tab_demo_tv2"

  13. android:layout_width="fill_parent"

  14. android:layout_height="fill_parent"

  15. android:text="tab_demo_tv2"

  16. />

  17. <TextViewandroid:id="@+id/tab_demo_tv3"

  18. android:layout_width="fill_parent"

  19. android:layout_height="fill_parent"

  20. android:text="tab_demo_tv3"

  21. />

  22. </FrameLayout>


2)TabDemo.java


[java] view plain copy
  1. publicclass TabDemo extends TabActivity {    

  2. private TabHost tabHost;    

  3. publicvoid onCreate(Bundle savedInstanceState) {    

  4. super.onCreate(savedInstanceState);    

  5.        tabHost = getTabHost();    

  6.        LayoutInflater.from(this).inflate(R.layout.tab_demo, tabHost.getTabContentView(), true);    

  7.        tabHost.addTab(tabHost.newTabSpec("tab1").setIndicator("Tab1", null).setContent(R.id.tab_demo_tv1));  

  8.        tabHost.addTab(tabHost.newTabSpec("tab2").setIndicator("Tab2", null).setContent(R.id.tab_demo_tv2));  

  9.        tabHost.addTab(tabHost.newTabSpec("tab3").setIndicator("Tab3", null).setContent(R.id.tab_demo_tv3));  

  10.        setContentView(tabHost);    

  11.    }    

  12. }    


2. TabHost绑定动态View(从2个Layout中取数据)

(1)效果图

8d100de9-9d1e-339b-ac6e-a5515cb6a65e.jpg

(2)代码

1)tab_map.xml


[xhtml] view plain copy
  1. <?xmlversion="1.0"encoding="utf-8"?>

  2. <FrameLayoutxmlns:android="http://schemas.android.com/apk/res/android"

  3. android:orientation="vertical"

  4. android:layout_width="fill_parent"

  5. android:layout_height="fill_parent"

  6. android:id="@+id/tab_map_id"

  7. >

  8. </FrameLayout>


2)tab_hs.xml


[xhtml] view plain copy
  1. <?xmlversion="1.0"encoding="utf-8"?>

  2. <FrameLayoutxmlns:android="http://schemas.android.com/apk/res/android"

  3. android:orientation="vertical"

  4. android:layout_width="fill_parent"

  5. android:layout_height="fill_parent"

  6. android:id="@+id/tab_hs_id"

  7. >

  8. <TextViewandroid:id="@+id/tab_hs_tv"

  9. android:layout_width="fill_parent"

  10. android:layout_height="fill_parent"

  11. />

  12. </FrameLayout>


3)MapView.java


[java] view plain copy
  1. publicclass MapView extends View {    

  2. public MapView(Context context) {    

  3. super(context);    

  4.    }    

  5. protectedvoid onDraw(Canvas canvas) {    

  6.        Paint p = new Paint();    

  7.        canvas.drawBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.icon), 0, 0, p);  

  8.    }    

  9. }    


4)TabDemo.java


[java] view plain copy
  1. publicclass TabDemo extends TabActivity {    

  2. private TabHost tabHost;    

  3. publicvoid onCreate(Bundle savedInstanceState) {    

  4. super.onCreate(savedInstanceState);    

  5.        tabHost = getTabHost();    

  6.        createTabSpec_map();    

  7.        createTabSpec_hs();    

  8.        setContentView(tabHost);    

  9.    }    

  10. privatevoid createTabSpec_map() {    

  11.        LayoutInflater inflater_tab1 = LayoutInflater.from(this);    

  12.        inflater_tab1.inflate(R.layout.tab_map, tabHost.getTabContentView());    

  13. /*

  14.         * R.layout.tab_demo已被LayoutInflater注册,所以这个可以通过findViewById获得其对象

  15.         */

  16.        FrameLayout frameLayout = (FrameLayout) findViewById(R.id.tab_map_id);    

  17.        MapView mv = new MapView(this);    

  18.        frameLayout.addView(mv);    

  19.        TabHost.TabSpec tabSpec_map = tabHost.newTabSpec("map view");    

  20.        tabSpec_map.setIndicator("map view", null);    

  21.        tabSpec_map.setContent(R.id.tab_map_id); // 动态绑定基于图片的View(通过一个Layout绑定)  

  22.        tabHost.addTab(tabSpec_map);    

  23.    }    

  24. privatevoid createTabSpec_hs() {    

  25.        LayoutInflater inflater_tab2 = LayoutInflater.from(this);    

  26.        inflater_tab2.inflate(R.layout.tab_hs, tabHost.getTabContentView());    

  27.        TabHost.TabSpec tabSpec_hs = tabHost.newTabSpec("hs view");    

  28.        tabSpec_hs.setIndicator("hs view");    

  29.        tabSpec_hs.setContent(R.id.tab_hs_id); // 绑定一个新的Layout  

  30.        tabHost.addTab(tabSpec_hs);    

  31. /*

  32.         * 这个绑定View的操作必须要重新使用一个新方法来完成,因为Tab的生成是在onCreate()中完成的,onCreate()只被调用一次,

  33.         * 但是数据更新操作是需要反复进行的,如果反复调用createTabSpec_hs()则会生成多个tab页,这是我们不希望的,所以单独把

  34.         * updata分离出来,数据更新时单独调用此方法就可以了。

  35.         */

  36.        updateTabSpec_hs();    

  37.    }    

  38. privatevoid updateTabSpec_hs() {    

  39.        TextView tv = (TextView) findViewById(R.id.tab_hs_tv);    

  40.        tv.setText("This is tab2");    

  41.    }    

  42. }