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

(1)效果图

(2)代码

1)tab_demo.xml

<?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

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

<?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

<?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

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

public class TabDemo extends TabActivity {      private TabHost tabHost;            public void onCreate(Bundle savedInstanceState) {          super.onCreate(savedInstanceState);          tabHost = getTabHost();                    createTabSpec_map();          createTabSpec_hs();                    setContentView(tabHost);      }            private void createTabSpec_map() {          LayoutInflater inflater_tab1 = LayoutInflater.from(this);          inflater_tab1.inflate(R.layout.tab_map, tabHost.getTabContentView());                    /*          * R.layout.tab_demo已被LayoutInflater注册,所以这个可以通过findViewById获得其对象          */          FrameLayout frameLayout = (FrameLayout) findViewById(R.id.tab_map_id);          MapView mv = new MapView(this);          frameLayout.addView(mv);                    TabHost.TabSpec tabSpec_map = tabHost.newTabSpec("map view");          tabSpec_map.setIndicator("map view", null);          tabSpec_map.setContent(R.id.tab_map_id); // 动态绑定基于图片的View(通过一个Layout绑定)          tabHost.addTab(tabSpec_map);      }      private void createTabSpec_hs() {          LayoutInflater inflater_tab2 = LayoutInflater.from(this);          inflater_tab2.inflate(R.layout.tab_hs, tabHost.getTabContentView());                    TabHost.TabSpec tabSpec_hs = tabHost.newTabSpec("hs view");          tabSpec_hs.setIndicator("hs view");          tabSpec_hs.setContent(R.id.tab_hs_id); // 绑定一个新的Layout          tabHost.addTab(tabSpec_hs);                    /*          * 这个绑定View的操作必须要重新使用一个新方法来完成,因为Tab的生成是在onCreate()中完成的,onCreate()只被调用一次,          * 但是数据更新操作是需要反复进行的,如果反复调用createTabSpec_hs()则会生成多个tab页,这是我们不希望的,所以单独把          * updata分离出来,数据更新时单独调用此方法就可以了。          */          updateTabSpec_hs();      }      private void updateTabSpec_hs() {          TextView tv = (TextView) findViewById(R.id.tab_hs_tv);          tv.setText("This is tab2");      }  }  

更多相关文章

  1. Android(安卓)onSaveInstanceState()和onRestoreInstanceState()
  2. android 团队开发技巧1 - activity 启动
  3. android bluetooth 移植相关注意事项
  4. android display
  5. Android(安卓)触摸事件处理机制
  6. android 首开机会在数据链接图标的状态栏打开并自行消失主动
  7. Android(安卓)TransactionTooLargeException 解析,思考与监控方案
  8. android 录音机&示波器
  9. mybatisplus的坑 insert标签insert into select无参数问题的解决

随机推荐

  1. 粗看 Android(安卓)4.0 界面变化
  2. Android性能优化-线程性能优化
  3. Android调试方法大全
  4. Android(安卓)Q 带来哪些变化
  5. 基于android的远程视频监控系统——实现,
  6. 自定义获取WI-FI列表及相关设置
  7. 真正的系统-系统与UI即任务的分离
  8. 深入浅析Android坐标系统
  9. android基础2——android工程目录结构
  10. Java->Android并发编程引气入门篇