您现在的位置:首页 > 博客 > Android开发 > UI开发 > 正文
Android TabHost的使用【转】
http://www.drovik.com/      2012-9-6 19:50:32      来源:CSDN社区      点击:

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

(1)效果图

(2)代码

1)tab_demo.xml

<?xml version="1.0" encoding="utf-8"?>
  1. <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. android:orientation="vertical"
  3. android:layout_width="fill_parent"
  4. android:layout_height="fill_parent"
  5. >
  6. <TextView android:id="@+id/tab_demo_tv1"
  7. android:layout_width="fill_parent"
  8. android:layout_height="fill_parent"
  9. android:text="tab_demo_tv1"
  10. />
  11. <TextView android:id="@+id/tab_demo_tv2"
  12. android:layout_width="fill_parent"
  13. android:layout_height="fill_parent"
  14. android:text="tab_demo_tv2"
  15. />
  16. <TextView android:id="@+id/tab_demo_tv3"
  17. android:layout_width="fill_parent"
  18. android:layout_height="fill_parent"
  19. android:text="tab_demo_tv3"
  20. />
  21. </FrameLayout>
<?xml version="1.0" encoding="utf-8"?> <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <TextView android:id="@+id/tab_demo_tv1" android:layout_width="fill_parent" android:layout_height="fill_parent" android:text="tab_demo_tv1" /> <TextView android:id="@+id/tab_demo_tv2" android:layout_width="fill_parent" android:layout_height="fill_parent" android:text="tab_demo_tv2" /> <TextView android:id="@+id/tab_demo_tv3" android:layout_width="fill_parent" android:layout_height="fill_parent" android:text="tab_demo_tv3" /> </FrameLayout>

2)TabDemo.java

  1. public class TabDemo extends TabActivity {
  2. private TabHost tabHost;
  3. public void 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. }
public class TabDemo extends TabActivity { private TabHost tabHost; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); tabHost = getTabHost(); LayoutInflater.from(this).inflate(R.layout.tab_demo, tabHost.getTabContentView(), true); tabHost.addTab(tabHost.newTabSpec("tab1").setIndicator("Tab1", null).setContent(R.id.tab_demo_tv1)); tabHost.addTab(tabHost.newTabSpec("tab2").setIndicator("Tab2", null).setContent(R.id.tab_demo_tv2)); tabHost.addTab(tabHost.newTabSpec("tab3").setIndicator("Tab3", null).setContent(R.id.tab_demo_tv3)); setContentView(tabHost); } }

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

(1)效果图

(2)代码

1)tab_map.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <FrameLayout xmlns: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>
<?xml version="1.0" encoding="utf-8"?> <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" android:id="@+id/tab_map_id" > </FrameLayout>

2)tab_hs.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <FrameLayout xmlns: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. <TextView android:id="@+id/tab_hs_tv"
  9. android:layout_width="fill_parent"
  10. android:layout_height="fill_parent"
  11. />
  12. </FrameLayout>
<?xml version="1.0" encoding="utf-8"?> <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" android:id="@+id/tab_hs_id" > <TextView android:id="@+id/tab_hs_tv" android:layout_width="fill_parent" android:layout_height="fill_parent" /> </FrameLayout>

3)MapView.java

  1. public class MapView extends View {
  2. public MapView(Context context) {
  3. super(context);
  4. }
  5. protected void onDraw(Canvas canvas) {
  6. Paint p = new Paint();
  7. canvas.drawBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.icon), 0, 0, p);
  8. }
  9. }
public class MapView extends View { public MapView(Context context) { super(context); } protected void onDraw(Canvas canvas) { Paint p = new Paint(); canvas.drawBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.icon), 0, 0, p); } }

4)TabDemo.java

  1. public class TabDemo extends TabActivity {
  2. private TabHost tabHost;
  3. public void onCreate(Bundle savedInstanceState) {
  4. super.onCreate(savedInstanceState);
  5. tabHost = getTabHost();
  6. createTabSpec_map();
  7. createTabSpec_hs();
  8. setContentView(tabHost);
  9. }
  10. private void createTabSpec_map() {
  11. LayoutInflater inflater_tab1 = LayoutInflater.from(this);
  12. inflater_tab1.inflate(R.layout.tab_map, tabHost.getTabContentView());
  13. FrameLayout frameLayout = (FrameLayout) findViewById(R.id.tab_map_id);
  14. MapView mv = new MapView(this);
  15. frameLayout.addView(mv);
  16. TabHost.TabSpec tabSpec_map = tabHost.newTabSpec("map view");
  17. tabSpec_map.setIndicator("map view", null);
  18. tabSpec_map.setContent(R.id.tab_map_id); // 动态绑定基于图片的View(通过一个Layout绑定)
  19. tabHost.addTab(tabSpec_map);
  20. }
  21. private void createTabSpec_hs() {
  22. LayoutInflater inflater_tab2 = LayoutInflater.from(this);
  23. inflater_tab2.inflate(R.layout.tab_hs, tabHost.getTabContentView());
  24. TabHost.TabSpec tabSpec_hs = tabHost.newTabSpec("hs view");
  25. tabSpec_hs.setIndicator("hs view");
  26. tabSpec_hs.setContent(R.id.tab_hs_id); // 绑定一个新的Layout
  27. tabHost.addTab(tabSpec_hs);
  28. updateTabSpec_hs();
  29. }
  30. private void updateTabSpec_hs() {
  31. TextView tv = (TextView) findViewById(R.id.tab_hs_tv);
  32. tv.setText("This is tab2");
  33. }
  34. }
分享到:
发表评论(0)
姓名 *
评论内容 *
验证码 *图片看不清?点击重新得到验证码