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

时间:2021-08-09 10:08:39 

首先,定义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 TabHost(选项卡)的使用方法

标签:TabHost,选项卡
0
投稿

猜你喜欢

  • C++实现约瑟夫环的循环单链表

    2022-11-12 19:34:29
  • java面试常见模式问题---单例模式

    2022-11-29 09:12:28
  • C#实现基于XML配置MenuStrip菜单的方法

    2023-03-06 21:48:50
  • c#使用win32api实现获取光标位置

    2022-05-09 10:59:33
  • 使用maven创建web项目的方法步骤(图文)

    2022-12-08 04:36:41
  • Java编程探索之泛型擦除实例解析

    2022-08-30 02:13:35
  • 浅谈@FeignClient中name和value属性的区别

    2023-11-06 13:04:14
  • 详解Java如何在CompletableFuture中实现日志记录

    2022-03-21 17:11:01
  • java泛型基本知识和通用方法

    2023-09-19 12:59:12
  • SSM项目使用拦截器实现登录验证功能

    2023-06-17 16:12:38
  • java对ArrayList排序代码示例

    2023-11-24 20:14:21
  • C#如何Task执行任务,等待任务完成

    2022-03-06 11:31:31
  • JAVA中对List进行查询

    2023-12-17 20:41:20
  • Mybatis查询时,区分大小写操作

    2021-08-11 14:10:54
  • C#将Unicode编码转换为汉字字符串的简单方法

    2021-06-05 02:11:38
  • Java编程中利用InetAddress类确定特殊IP地址的方法

    2021-06-24 23:00:12
  • C#实现调用迅雷下载的方法

    2022-03-29 12:42:21
  • Spring如何使用@Indexed加快启动速度

    2022-05-02 10:50:40
  • FeignClient中name和url属性的作用说明

    2023-06-04 13:21:55
  • Spring boot2X负载均衡和反向代理实现过程解析

    2023-02-06 04:18:53
  • asp之家 软件编程 m.aspxhome.com