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

(1)效果图

(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)效果图

(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. }    



更多相关文章

  1. Android传感器的介绍
  2. android 问题汇总系列之五
  3. Android(安卓)页面跳转数据传递
  4. Android(安卓)开发入门-使用 Intent 在活动之间穿梭
  5. Android移动应用开发——用户注册
  6. Android(安卓)ANR keyDispatchingTimedOut Error while continuo
  7. Android(安卓)选择文件并加载数据到界面
  8. android花屏效果的实现(ViewPager的基本使用)
  9. Android(安卓)的一些提示框

随机推荐

  1. android hook getdeceiveid
  2. Android中创建对话框
  3. Android(安卓)Stduio出现“required plug
  4. android ExpandableListView ExpandableL
  5. pandaboard ES学习之旅——4 Android源代
  6. Android 中文 API (29) ―― CompoundButto
  7. 跟着做 Android NDK学习入门如此简单(二)
  8. android数据存取的四种方式
  9. 在activity中调用Application 出现androi
  10. Android——编译release版签名系统