android TabHost(选项卡)的使用方法

 更新时间:2013年11月19日 11:18:36   作者:  
Android中的选项卡是用TabHost实现的,下面我们用一个例子说明他的使用方法

首先,定义TabHost的布局文件:

复制代码 代码如下:

<?xml version="1.0" encoding="utf-8"?>

<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/tabhost" 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 android:id="@android:id/tabs"
            android:layout_alignParentBottom="true"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"/>

        <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent" />

    </LinearLayout>

</TabHost>

其中,TabWidget即是选项卡上面的标签,FrameLayout是选项卡的内容。
在Java类文件中定义如下:

复制代码 代码如下:

public class MainActivity extends TabActivity {

    private TabHost my_tabhost; 
    private TabWidget my_tabwidget;
    private int i,k;
    private TextView tv;

    private String[] tabMenu = { "系统", "硬件", "操作"};
    private Intent intent0, intent1, intent2;
    private Intent[] intents = { intent0, intent1, intent2};
    private TabHost.TabSpec tabSpec0, tabSpec1, tabSpec2, tabSpec3;
    private TabHost.TabSpec[] tabSpecs = { tabSpec0, tabSpec1, tabSpec2, tabSpec3};

    public static Context mContext;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
                // 不要窗体标题
               requestWindowFeature(Window.FEATURE_NO_TITLE);
               setContentView(R.layout.activity_main);
        setContentView(R.layout.activity_main);

        my_tabhost = getTabHost();

        intent0 = new Intent(this, system.class);
        intent1 = new Intent(this, hardware.class);
        intent2 = new Intent(this, operation.class);

        tabSpec0 = my_tabhost.newTabSpec("system").setIndicator(tabMenu[0],null).
                setContent(intent0);
        tabSpec1 = my_tabhost.newTabSpec("hardware").setIndicator(tabMenu[1],null).
                setContent(intent1);
        tabSpec2 = my_tabhost.newTabSpec("operation").setIndicator(tabMenu[2],null).
                setContent(intent2);

        my_tabhost.addTab(tabSpec1);
        my_tabhost.addTab(tabSpec0);
        my_tabhost.addTab(tabSpec2);
<br>                 // 设置默认选中的选项卡为第2个      
                     my_tabhost.setCurrentTab(1);

    }

}

每一个选项卡对应一个Intent,每一个Intent又对应一个类,选中这个选项卡时,就显示对应的类。
运行结果如下:

相关文章

  • android的got表HOOK实现代码

    android的got表HOOK实现代码

    对于android的so文件的hook根据ELF文件特性分为:Got表hook、Sym表hook和inline hook等。今天通过本文给大家介绍android HOOK实现got表的实例代码,需要的朋友参考下吧
    2021-08-08
  • 利用Android设计一个倒计时组件

    利用Android设计一个倒计时组件

    在很多电商工作项目中经常有倒计时的场景,比如活动倒计时、抢红包倒计时等等,今天小编就带大家来学习如何利用Android设计倒计时组件,感兴趣的小伙伴一起奥
    2021-09-09
  • Android性能优化之JVMTI与内存分配

    Android性能优化之JVMTI与内存分配

    这篇文章主要为大家介绍了Android性能优化之JVMTI与内存分配,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2022-10-10
  • Android便携式热点的开启状态检测和SSID的获取方法

    Android便携式热点的开启状态检测和SSID的获取方法

    WIFI热点的开启状态和开启后的SSID如何获取呢?接下来通过本文给大家分享Android便携式热点的开启状态检测和SSID的获取方法,需要的朋友参考下吧
    2017-01-01
  • Android中Intent传递对象的3种方式详解

    Android中Intent传递对象的3种方式详解

    这篇文章给大家介绍了Android中Intent传递对象的3种方式,分别是Serializable 方式、Parcelable 方式以及JSON 方式,有需要的朋友们可以一起参考借鉴,下面来一起看看吧。
    2016-09-09
  • Android videoview抢占焦点的处理方法

    Android videoview抢占焦点的处理方法

    这篇文章主要为大家详细介绍了Android videoview抢占焦点的处理方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-06-06
  • android实现okHttp的get和post请求的简单封装与使用

    android实现okHttp的get和post请求的简单封装与使用

    这篇文章主要介绍了android实现okHttp的get和post请求的简单封装与使用,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2021-05-05
  • Android中外接键盘的检测的实现

    Android中外接键盘的检测的实现

    这篇文章主要介绍了Android中外接键盘的检测的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2019-11-11
  • android实现弹出提示框

    android实现弹出提示框

    这篇文章主要为大家详细介绍了android实现弹出提示框,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2021-01-01
  • Android Studio获取网络JSON数据并处理的方法

    Android Studio获取网络JSON数据并处理的方法

    这篇文章主要为大家详细介绍了Android Studio获取网络JSON数据并处理的方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-10-10

最新评论