[Android] 选项卡组件TabHost_android tabhost-程序员宅基地

技术标签: android-studio  Android 开发  

Tab选项卡实现多个分页之间的快速切换,每个分页可以显示不同的内容,在Android平台提供了TabHost组件实现Tab选项卡的功能,选项卡组件的主要功能是可以进行应用程序分类管理。
每个选项卡称为一个Tab,而包含这多个选项卡的容器称为TabHost

1.TabHost的常用方法

  • public TabHost (Context context): 创建TabHost类对象
  • public void addTab(TabHost.TabSpec tabSpec):增加一个tab
  • public TabHost.TabSpec newTabSpec(String tag):创建一个TabHost.TabSpec对象
  • public void clearAllTabs():清除所有关联到当前TabHost的选项卡
  • public View getCurrentView():取得当前的View对象
  • public int getCurrentTab():获取当前选项卡的ID
  • public String getCurrentTabTag():获得当前选项卡的Tag选项内容
  • public View getCurrentTabView():获取当前选项卡的视图
  • public TabHost.TabSpec newTabSpec(String tag):获取一个TabHost.TabSpec,并关联到当前TabHost
  • public void setup():建立TabHost对象
  • public void setCurrentTab(int index ):设置当前显示的Tab编号
  • public void setCurrentTabByTag(String tag):设置当前显示的Tab名称
  • public FrameLayout getTabContentView():获取并保存选项卡内容
  • public void setOnTabChangedListener(TabHost.OnTabChangeListener l):设置选项改变时触发
  • public void setup():使用findViewById()加载TabHost,在新增一个选项卡之前,需要调用它

如果要实现选项卡的显示界面,有两种实现途径:
1、直接让一个Activity程序继承TabActivity类;该方法已经废弃,不建议使用。
2、在布局文件中使用TabHost,无需继承TabActivity。
1) 在界面布局中定义TabHost组件,并为该组件定义选项卡内容
2 )使用findViewById ( )获取TabHost组件
3 )通过TabHost对象的方法来创建和添加选项卡
注意:
如果使用findViewById()方法取得TabHost组件,那么在新增一个选项卡之前,需要调用setup()方法来建立一个TabHost对象。
在TabActivity里使用getTabHost()方法获取TabHost组件,就不需要调用setup()方法。(废弃)

如果要增加一个选项卡,则需要使用方法addTab(TabHost tabhost, TabSpec tabspec),如果有多个选项就要增加多个TabHost. TabSpec对象。
TabHost. TabSpec是TabHost定义的内部类,如果要想取得此类的实例化对象,就需要依靠TabHost类的newTabSpec()来完成。
每个选项卡都包含一个选项卡指示符、内容和tag选项。

2.TabHost.TabSpec类定义的常用方法

  • public TabHost.TabSpec setContent(int viewId):设置要显示的组件ID
  • public TabHost.TabSpec setContent(Intent intent):指定一个加载activity的Intent对象作为选项卡内容
  • public TabHost.TabSpec setContent(TabHost.TabContentFactory contentFactory):指定TabHost.TabContentFactory用于创建选项卡的内容
  • public TabHost.TabSpec setIndicator(View view):指定一个视图作为选项卡指示符
  • public TabHost.TabSpec setIndicator(CharSequence label ):设置一个选项
  • public TabHost. setIndicator (ChaSequence label ,Drawable icon):为选项卡指定符指定一个选项和图标
  • public String getTag():获取tag选项字符串

对配置文件的编写有以下要求。
(1)所有用于选项配置的布局文件,必须以“”为根节点。
(2)为保证选项页和选项内容显示正常,可以采用一个布局管理器进行布局。
(3)定义一个“< TagWidget >”组件,用于表示整个选项容器。该组件的android:id=“@android:id/tabs”。
(4)定义选项页必须使用FrameLayout布局,而后在布局中定义所需要的选项页组件, 且框架布局中必须引用tabcontent组件 (android:id=“@android:id/tabcontent”)。
TabHost是整个Tab的容器,包括两部分:TabWidget和FrameLayout。其中 TabWidget:每个Tab的选项,即Tab标签;
FrameLayout:用来显示每个Tab选项切换时具体显示的内容。

3.TabWidget类常用属性

  • android: divider: 可绘制对象,被绘制在选项卡窗口间充当分割物
  • android: tabStripEnabled :确定是否在选项卡中绘制
  • android: tabStripLeft:用来绘制选项卡下面的分割线左边部分的可视化对象
  • android:tabStripRight: 用来绘制选项卡下面的分割线右边部分的可视化对象

4.TabWidget 类常用方法

  • public TabWidget (Context context ):创建TabWidget 实例
  • public void addView(View child):向TabWidget增加组件
  • public int getTabCount(): 返回选项卡的数量
  • public void setEnabled(boolean enabled):配置是否启用
  • public void focusCurrentTab(int index ):设置当前选项卡并且使其获得焦点
  • public void setCurrentTab(int index):设置当前选项卡

5.运行图

在这里插入图片描述

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

6.运行图

(1)activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:id="@+id/tabhost"
    tools:context=".MainActivity">
    <!--顶部导航-->
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
        <!--     TabWidget   每个Tab的选项,即Tab标签-->
        <TabWidget
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@android:id/tabs"
            android:layout_alignParentTop="true"/>
        <FrameLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@android:id/tabcontent">
            <!--Tab页1-->
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:id="@+id/file"
                android:gravity="center_horizontal"
                android:orientation="vertical">
                <Button
                    android:layout_width="200dp"
                    android:layout_height="wrap_content"
                    android:id="@+id/open"
                    android:text="打开文件"/>
                <Button
                    android:layout_width="200dp"
                    android:layout_height="wrap_content"
                    android:id="@+id/save"
                    android:text="保存文件"/>
                <Button
                    android:layout_width="200dp"
                    android:layout_height="wrap_content"
                    android:id="@+id/saveAs"
                    android:text="文件另存为"
                    />
            </LinearLayout>
            <!--Tab页2-->
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:id="@+id/edit"
                android:gravity="center_horizontal"
                android:orientation="vertical"
                >
                <Button
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:id="@+id/copy"
                    android:text="复制"/>
                <Button
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:id="@+id/paste"
                    android:text="粘贴"
                    />
            </LinearLayout>
            <!--Tab页3-->
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:id="@+id/seek"
                android:gravity="center_horizontal"
                android:orientation="vertical">
                <EditText
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:id="@+id/edit1"
                    android:text="请输入检索关键字…"
                    android:textSize="18sp"/>
                <Button
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:id="@+id/seekbut1"
                    android:text="搜索"/>
            </LinearLayout>
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:id="@+id/time"
                android:gravity="center_horizontal"
                android:orientation="vertical">
                <TimePicker
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:id="@+id/seekbut"
                    android:text="设置时间"/>
            </LinearLayout>

        </FrameLayout>
    </LinearLayout>

</TabHost>

(2)MainActivity.java

package com.example.progressdialog;

import androidx.appcompat.app.AppCompatActivity;


import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TabHost;
import android.widget.Toast;


public class MainActivity extends AppCompatActivity {
    
    private TabHost mytabHost; //定义TabHost
    private int[]layRes={
    R.id.file,R.id.edit,R.id.seek,R.id.time};//定义内嵌布局管理器ID
    private Button openButton,save,saveAs,copy,paste,seekbut1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main); //调用默认布局管理器
        mytabHost=findViewById(R.id.tabhost);//取得TabHost对象
        mytabHost.setup(); //建立TabHost对象
        TabHost.TabSpec myTab1=mytabHost.newTabSpec("文件"); //定义TabSpec
        myTab1.setIndicator("文件"); //设置选项文字
        myTab1.setContent(layRes[0]);  //设置显示的组件
        mytabHost.addTab(myTab1); //增加选项

        TabHost.TabSpec myTab2=mytabHost.newTabSpec("编辑");//定义TabSpec
        myTab2.setIndicator("编辑").setContent(layRes[1]); //设置显示的组件
        mytabHost.addTab(myTab2);  //增加选项

        TabHost.TabSpec myTab3=mytabHost.newTabSpec("查看");//定义TabSpec
        myTab3.setIndicator("查看").setContent(layRes[2]); //设置显示的组件
        mytabHost.addTab(myTab3);  //增加选项

        TabHost.TabSpec myTab4=mytabHost.newTabSpec("设置");//定义TabSpec
        myTab4.setIndicator("设置").setContent(layRes[3]); //设置显示的组件
        mytabHost.addTab(myTab4);  //增加选项

        mytabHost.setCurrentTab(0);                //设置开始默认选项
//        myTabHost.setCurrentTabByTag("文件");

        openButton=findViewById(R.id.open);
        save=findViewById(R.id.save);
        saveAs=findViewById(R.id.saveAs);
        copy=findViewById(R.id.copy);
        paste=findViewById(R.id.paste);
        seekbut1=findViewById(R.id.seekbut1);

        openButton.setOnClickListener(new MyOnClickListener());
        save.setOnClickListener(new MyOnClickListener());
        saveAs.setOnClickListener(new MyOnClickListener());
        copy.setOnClickListener(new MyOnClickListener());
        paste.setOnClickListener(new MyOnClickListener());
        seekbut1.setOnClickListener(new MyOnClickListener());


    }
    private class MyOnClickListener implements View.OnClickListener {
    
        public void onClick(View v) {
    
            switch (v.getId()) {
    
                case R.id.open:
                    Toast.makeText(MainActivity.this,
                            "这是打开文件的按钮", Toast.LENGTH_SHORT).show();
                    break;
                case R.id.save:
                    Toast.makeText(MainActivity.this,
                            "这是保存文件的按钮", Toast.LENGTH_SHORT).show();
                    break;
                case R.id.saveAs:
                    Toast.makeText(MainActivity.this,
                            "这是文件另存为的按钮", Toast.LENGTH_SHORT).show();
                    break;
                case R.id.copy:
                    Toast.makeText(MainActivity.this,
                            "这是复制的按钮", Toast.LENGTH_SHORT).show();
                    break;
                case R.id.paste:
                    Toast.makeText(MainActivity.this,
                            "这是粘贴的按钮", Toast.LENGTH_SHORT).show();
                    break;
                case R.id.seekbut1:
                    Toast.makeText(MainActivity.this,
                            "这是搜索的按钮", Toast.LENGTH_SHORT).show();
                    break;

            }
        }
    }
}

实现底部导航运行图,再写一个布局文件,不妨命名为activity_second.xml,代码如下所示,在MainActivity.java中,将 setContentView(R.layout.activity_main);改为
setContentView(R.layout.activity_second)即可

(3)activity_second.xml

<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/tabhost"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    <!--实现底部导航-->
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1">

            <LinearLayout
                android:id="@+id/file"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:gravity="center_horizontal"
                android:orientation="vertical">

                <Button
                    android:id="@+id/open"
                    android:layout_width="150dp"
                    android:layout_height="wrap_content"
                    android:text="打开文件" />

                <Button
                    android:id="@+id/save"
                    android:layout_width="150dp"
                    android:layout_height="wrap_content"
                    android:text="保存文件" />

                <Button
                    android:id="@+id/saveAs"
                    android:layout_width="150dp"
                    android:layout_height="wrap_content"
                    android:text="文件另存为" />
            </LinearLayout>

            <LinearLayout
                android:id="@+id/edit"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:gravity="center_horizontal"
                android:orientation="vertical">

                <Button
                    android:id="@+id/copy"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="复制" />

                <Button
                    android:id="@+id/paste"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="粘贴" />
            </LinearLayout>

            <LinearLayout
                android:id="@+id/seek"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:gravity="center_horizontal"
                android:orientation="vertical">

                <EditText
                    android:id="@+id/edit1"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="请输入检索关键字..."
                    android:textSize="18sp" />

                <Button
                    android:id="@+id/seekbut1"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="搜索" />
            </LinearLayout>

            <LinearLayout
                android:id="@+id/time"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:gravity="center_horizontal"
                android:orientation="vertical">

                <TimePicker
                    android:id="@+id/seekbut"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="设置时间" />
            </LinearLayout>
        </FrameLayout>

        <TabWidget
            android:id="@android:id/tabs"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_weight="0" />
    </LinearLayout>
</TabHost>


在这里插入图片描述

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/sam___1/article/details/124390418

智能推荐

2021年江苏省职业院校技能大赛中职 “网络信息安全”赛项(超详细)_2021年江苏省职业院校技能大赛网络信息安全赛项中职组-程序员宅基地

文章浏览阅读2.1k次,点赞3次,收藏6次。2021年中职组“网络空间安全”赛项一.江苏省竞赛任务书二.任务书解析:三.不懂的可以私信博主!一.江苏省竞赛任务书一、竞赛时间8:00—11:00 共计3小时二、竞赛阶段竞赛阶段 任务阶段 竞赛任务 竞赛时间 分值第①阶段:单兵模式系统渗透测试任务一:攻击日志分析任务二:系统漏洞利用与提权任务三:代码审计任务四:web安全渗透测试任务五:Linux操作系统渗透测试任务六:端口扫描Python渗透测试备战阶段 休息20分钟第②阶段:分组对抗 系统加固 15分钟_2021年江苏省职业院校技能大赛网络信息安全赛项中职组

Python3爬取猫眼电影信息-程序员宅基地

文章浏览阅读1k次。Python3爬取猫眼电影信息import jsonimport requestsfrom requests.exceptions import RequestExceptionimport reimport time#爬取猫眼电影信息def get_one_page(url): #增加了User-Agent识别,需要在headers中添加User-Agent参数。 ..._python3爬取猫眼

初识Docker(基本概念)_if [ type docker]-程序员宅基地

文章浏览阅读620次。文章目录前言什么是Docker&为什么用它Docker基本概念Docker镜像Docker与虚拟机的差别DockerHub容器总结前言兄弟们,明天我就期末考试了,给我来一波祝福~!!!老规矩,什么是Docker,这玩意能干嘛,有什么用,为什么要用它,有什么优势。在学习一个新东西之前我们都需要带着这些疑问开始进入学习,那么今天的这篇博客主要就是讲解一些基本的概念,这样一来对于Docker的学习就非常快了,同时这也是我本人的笔记整理。那么接下来就开始一一解答上面提到的问题。本章节为纯理论,后面才是_if [ type docker]

将datatable转化为list_datatable杞塋ist-程序员宅基地

文章浏览阅读1.3k次。定义一个person类,声明属性id,name.addresspublic class Person { private int id; public int Id { get { return id; } set { id = value; } } _datatable杞塋ist

ARM Cortex-M0(1)---浅谈ARM Cortex-M0_m0的体系结构-程序员宅基地

文章浏览阅读6.2k次。浅谈ARM Cortex-M0 一、引言  ARM公司在2009年初发布了其嵌入式处理器系列中最小型、最低功耗的CortexM0处理器。CortexM0低功耗、高性能与极精简程序代码的特性,能应用于各种微控制器(MCU)中,并可让研发业者以8位的价位创造32位的效能,并进一步将传统的8位和16位的处理器推进至更高效能、更低功耗的32位处理器。二、关于CortexM0  AR..._m0的体系结构

Flash外部配置器件在SOPC中的应用_sopc中为什么加上flash就什么现象也没了-程序员宅基地

文章浏览阅读728次。1 Flash在SOPC中的作用   Flash在SOPC中的作用主要表现在两方面:一方面,可用Flash来保存FPGA的配置文件,从而可以省去EPCS芯片或解决EPCS芯片容量不够的问题。当系统上电后,从Flash中读取配置文件,对FPGA进行配置。另一方面,可用Flash来保存用户程序。对于较为复杂的SOPC系统,用户程序一般较大,用EPCS来存储是不现实的。系统完成配置后,将Flash中的用户程序转移到外接RAM或片内配置生成的RAM中,然后系统开始运行。  2 Flash编程_sopc中为什么加上flash就什么现象也没了

随便推点

使用Unity的50个建议_unity游戏中的一些规范和优化建议-程序员宅基地

文章浏览阅读491次。关于这些建议这些建议并不适用于所有的项目这些建议是基于我与3-20人的小团队项目经验总结出来的结构、可重复使用性、明晰度都是有价的——团队规模和项目规模决定了是否值得付这个价。一些建议也许公然违抗了传统的Unity开发。例如:使用专业化的组合而不是使用实例就很不像Unity的作风,价格也很高。即使看上去挺疯狂的,但我还是看到了这些建议给开发者带来了利益。 过程方面_unity游戏中的一些规范和优化建议

iOS与前端交互WKWebViewJavascriptBridge-程序员宅基地

文章浏览阅读2.5k次。废话不多说,直接进入正题~~~首先引入 pod 'WKWebViewJavascriptBridge'这部分代码是写死的主要用的是这部分代码 主要用的是这部分代码 主要用的是这部分代码 可以直接放在script标签下,也可以创建一个bridge.js function setupWKWebViewJavascriptBridge(callback) { if (window.WKWebViewJavascriptBridge..._wkwebviewjavascriptbridge

提高ASP.Net应用程序性能的十大方法-程序员宅基地

文章浏览阅读69次。转自:http://www.xiedaima.cn/read.php/72.htm提高asp.net应用程序性能的常说的神话 有用的提高asp.net应用程序性能的技巧 Asp.net应用程序操作数据库的建议 Asp.net中的缓存与后台处理进程 现在写一个asp.net的web应用程序变得非常的简单,许多的程序员都不愿花时间去构建一个性能良好的应用程序。本文将要讨论...

java reduce 求和_Java Stream流之求和的实现-程序员宅基地

文章浏览阅读2.2k次。BigDecimal:BigDecimal bb =list.stream().map(Plan::getAmount).reduce(BigDecimal.ZERO,BigDecimal::add);int、double、long:double max = list.stream().mapToDouble(User::getHeight).sum();补充:java8-Stream流之数值函数..._java reduce求和

aes_descryptoserviceprovider 中的密钥是8位; rijndaelmanaged -程序员宅基地

文章浏览阅读132次。Cannot find any provider supporting AES/CBC/PKCS7Padding关于“Cannot find any provider supporting AES/ECB/PKCS7Padding”问题的解决方案commons-codec-1.7.jarCommons Codec 1.14 Download bcprov-ext-jdk16-1.4..._descryptoserviceprovider 中的密钥是8位; rijndaelmanaged 中的密钥是32位。

AI一分钟 | 别了老司机!深圳无人驾驶公交车霸气上路;乌镇上演最强饭局,丁磊王兴刘强东大宴宾客-程序员宅基地

文章浏览阅读986次。一分钟AI美剧《硅谷》再现NIPS大会,英特尔请来Flo Rida暖场AI演讲互联网大佬的乌镇饭局刷爆朋友圈博通正式启动恶意收购:周一提交高通新董事提名深圳无人驾驶公交车上路试运营李彦宏称无人驾驶将会消灭酒驾雷军:AI将全面赋能小米的各项场景和服务刘强东乌镇发言:要在四川建185个无人机机场工信部:中国人工智能产业初步

推荐文章

热门文章

相关标签