第一步配置布局文件


<TabHost        android1:id="@android:id/tabhost"        android1:layout_width="match_parent"        android1:layout_height="match_parent"        android1:layout_weight="1" >        <LinearLayout            android1:layout_width="match_parent"            android1:layout_height="match_parent"            android1:orientation="vertical" >            <TabWidget                android1:id="@android:id/tabs"                android1:layout_width="match_parent"                android1:layout_height="wrap_content" >            </TabWidget>            <FrameLayout                android1:id="@android:id/tabcontent"                android1:layout_width="match_parent"                android1:layout_height="match_parent" >                <LinearLayout                    android1:id="@+id/tab1"                    android1:layout_width="match_parent"                    android1:layout_height="match_parent" >                    <ListView                        android1:id="@+id/listview1"                        android1:layout_width="fill_parent"                        android1:layout_height="fill_parent" >                    </ListView>                </LinearLayout>                <LinearLayout                    android1:id="@+id/tab2"                    android1:layout_width="match_parent"                    android1:layout_height="match_parent" >                </LinearLayout>            </FrameLayout>        </LinearLayout>    </TabHost>
上面这些大部分照抄即可,关键点在
<LinearLayout                    android1:id="@+id/tab2"                    android1:layout_width="match_parent"                    android1:layout_height="match_parent" >                </LinearLayout>


在linearlayout中设置你需要的控件,我这里放了一个listview

第二步 配置activity

final TabHost tabhost = (TabHost) findViewById(android.R.id.tabhost);tabhost.setup();TabWidget tabwidget = tabhost.getTabWidget();tabhost.addTab(tabhost.newTabSpec("tab1").setIndicator("tab1").setContent(R.id.tab1));tabhost.addTab(tabhost.newTabSpec("tab2").setIndicator("tab2").setContent(R.id.tab2));
我这里新建了两个tab,如果要继续增加根据上面的规律添加即可!

第三步 事件监听

tabhost.setOnTabChangedListener(new OnTabChangeListener() { public void onTabChanged(String arg0) { // TODO Auto-generated method stub Toast.makeText(DiquActivity.this, "当前标签页为" + arg0, Toast.LENGTH_SHORT).show(); } });tabhost.getTabWidget().getChildAt(0).setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {// TODO Auto-generated method stubtabhost.setCurrentTab(0);Toast.makeText(DiquActivity.this, "当前标签页为1",Toast.LENGTH_SHORT).show();}});tabhost.getTabWidget().getChildAt(1).setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {// TODO Auto-generated method stubtabhost.setCurrentTab(1);Toast.makeText(DiquActivity.this, "当前标签页为2",Toast.LENGTH_SHORT).show();}});
很容易懂吧,我就不解释了。照着做即可。android4.0环境下通过。




更多相关文章

  1. 安全新手入坑——HTML标签
  2. Android(安卓)百分比布局详解及实例代码
  3. Android动画四种类型xml布局
  4. 采用Scroller实现任意布局的上下左右弹性效果
  5. CheckBox控件中background和button的区别
  6. Android(安卓)ZoomControls缩放控件
  7. lint工具改错
  8. Android控件之ImageButton
  9. android Caused by: java.lang.IllegalArgumentException: Binar

随机推荐

  1. Android压力测试快速入门教程(图解)——Mon
  2. Android基础——通过activity和XML绑定实
  3. Android(安卓)ListFragment
  4. 一半是天使一半是魔鬼的Unsafe类详解(疫情
  5. 泛型就这么简单
  6. 运维人员的PMP考证路上心路历程
  7. Android中MVC框架的运用
  8. 从零开始写项目【总结】
  9. android 属性系统代码分析
  10. Android中显示html标签或者带图片