一、什么是TabHost。
Android 里面的TabHost就是选项卡,看下图(新浪微博界面):

Android TabHost学习笔记




至于选项卡有什么好处或者用途,我想代码哥们都知道吧,我就不多说了。

二、在Android里面如何实现TabHost
有两种方式可以实现。

1、继承TabActivity,然后用getTabHost()方法获取TabHost,最后在布局文件中定义各个Tab选项卡添加到TabHost中

Android TabHost学习笔记



2、不继承TabActivity,然后在布局文件中定义TabHost,最后讲各个Tab选项卡添加到TabHost中


总结以上两种方式为两步:

①:获取TabHost对象

②:把Tab添加到TabHost中。


我们先看第一种实现:

①:布局文件:

<?xml version="1.0" encoding="utf-8"?><LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"><!-- 第一个选项卡面板 --><LinearLayoutandroid:id="@+id/tab1"android:layout_width="fill_parent"android:layout_height="fill_parent"><!-- 面板中只有一个TextView--><TextView android:id="@+id/V1"android:layout_width="fill_parent"android:layout_height="fill_parent"android:text="Touch Android"/></LinearLayout><!-- 第二个选项卡面板 --><LinearLayoutandroid:id="@+id/tab2"android:layout_width="fill_parent"android:layout_height="fill_parent"><!-- 面板中只有一个TextView--><TextView android:id="@+id/V2"android:layout_width="fill_parent"android:layout_height="fill_parent"android:text="Touch Android"/></LinearLayout></LinearLayout>


②:Activity

package com.droidstouch.tabhost;import android.app.TabActivity;import android.os.Bundle;import android.view.LayoutInflater;import android.widget.TabHost;import android.widget.TabHost.TabSpec;/*** @author <a href="http://bbs.droidstouch.com">Touch Android</a>**/public class Demo2Activity extends TabActivity{protected void onCreate(Bundle savedInstanceState){super.onCreate(savedInstanceState);// this.setContentView(R.layout.demo2); // 注意不要加上此行代码//获取到TabHost对象TabHost tabHost =this.getTabHost();//把我们的布局文件添加到tabHost 的FrameLayout下面LayoutInflater.from(this).inflate(R.layout.demo2, tabHost.getTabContentView(), true);// 下面定义了两个选项卡//获取一个新的TabHost.TabSpec,并关联到当前tab host//参数:所需的选项卡标签TabSpec pSpec = tabHost.newTabSpec("parent");// 参数一:选项卡上的文字,参数二:选项卡的背景图片pSpec.setIndicator("父类", this.getResources().getDrawable(R.drawable.f_root));//设置选项卡内容pSpec.setContent(R.id.tab1);TabSpec subSpec = tabHost.newTabSpec("sub");subSpec.setIndicator("子类", this.getResources().getDrawable(R.drawable.f_sub));subSpec.setContent(R.id.tab2);// 将选项卡添加到TabHost中tabHost.addTab(pSpec);tabHost.addTab(subSpec);}}


第二种方式
①:布局文件

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:orientation="vertical"android:layout_width="fill_parent"android:layout_height="fill_parent"><!-- 定义TabHost ,自定义的TabHost一定得包含TabWidget 和 FrameLayout,并且 TabWidget 的ID一定是@android:id/tabsFrameLayout 的Id一定是@android:id/tabcontent--><TabHost android:id="@+id/tabs"android:layout_width="fill_parent"android:layout_height="fill_parent"><LinearLayout android:orientation="vertical"android:layout_width="fill_parent"android:layout_height="fill_parent"><!-- 定义TabWidget,此控件 必须和TabHost一起使用 --><TabWidget android:id="@android:id/tabs"android:layout_width="fill_parent"android:layout_height="wrap_content"/><!-- 定义FrameLayout--><FrameLayout android:id="@android:id/tabcontent"android:layout_width="fill_parent"android:layout_height="fill_parent"><TextView android:id="@+id/txtV1"android:layout_width="fill_parent"android:layout_height="fill_parent"android:text="Touch Android"/><TextView android:id="@+id/txtV2"android:layout_width="fill_parent"android:layout_height="fill_parent"android:text="http://bbs.droidstouch.com"/></FrameLayout></LinearLayout></TabHost></LinearLayout>


②:Activity:

package com.droidstouch.tabhost;import android.app.Activity;import android.os.Bundle;import android.widget.TabHost;import android.widget.TabHost.TabSpec;public class Dome1Activity extends Activity {/** Called when the activity is first created. */@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.demo1);//从布局文件中 获取到TabHostTabHost tabHost = (TabHost) this.findViewById(R.id.tabs);//安装TabHosttabHost.setup();// 下面定义两个选项卡//获取一个新的TabHost.TabSpec,并关联到当前tab host//参数:所需的选项卡标签TabSpec pSpec = tabHost.newTabSpec("parent");pSpec.setIndicator("父类", this.getResources().getDrawable(R.drawable.f_root));pSpec.setContent(R.id.txtV1);TabSpec subSpec = tabHost.newTabSpec("sub");subSpec.setIndicator("子类", this.getResources().getDrawable(R.drawable.f_root));subSpec.setContent(R.id.txtV2);//添加选项卡到TabHost中tabHost.addTab(pSpec);tabHost.addTab(subSpec);}}

更多相关文章

  1. Android之Manifest文件
  2. 自定义SeekBarPreference控件(老外出品,直接在preferences文件中
  3. android实现自定义相机以及图片的水印
  4. Android基础控件——ProgressBar自定义的介绍、动画效果实现、附
  5. Android资源文件 - 使用资源存储字符串 颜色 尺寸 整型 布尔值
  6. Android如何使用so文件和Android studio中导入so
  7. android 自定义ratingbar 图片显示不全的解决方案
  8. Android-Layout布局

随机推荐

  1. Android(安卓)菜单(OptionMenu)大全 建立
  2. 怎么在android的XML文件里加入凝视
  3. Xamarin为Mono for Android提供了一个可
  4. Android获取网速和下载速度
  5. android 开发-文件存储之读写sdcard
  6. dex.force.jumbo和Java heap space的问题
  7. android图片浏览器 PhotoStore
  8. Android(安卓)APK反编译详解(附图)
  9. cdr怎么画迅速去污图标? cdr去污logo简笔
  10. ubuntu apt-get install xxx报错无法下载