[Android] 选项卡组件TabHost

article/2025/9/9 21:45:09

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"><!--顶部导航--><LinearLayoutandroid:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"><!--     TabWidget   每个Tab的选项,即Tab标签--><TabWidgetandroid:layout_width="match_parent"android:layout_height="wrap_content"android:id="@android:id/tabs"android:layout_alignParentTop="true"/><FrameLayoutandroid:layout_width="match_parent"android:layout_height="match_parent"android:id="@android:id/tabcontent"><!--Tab页1--><LinearLayoutandroid:layout_width="match_parent"android:layout_height="match_parent"android:id="@+id/file"android:gravity="center_horizontal"android:orientation="vertical"><Buttonandroid:layout_width="200dp"android:layout_height="wrap_content"android:id="@+id/open"android:text="打开文件"/><Buttonandroid:layout_width="200dp"android:layout_height="wrap_content"android:id="@+id/save"android:text="保存文件"/><Buttonandroid:layout_width="200dp"android:layout_height="wrap_content"android:id="@+id/saveAs"android:text="文件另存为"/></LinearLayout><!--Tab页2--><LinearLayoutandroid:layout_width="match_parent"android:layout_height="match_parent"android:id="@+id/edit"android:gravity="center_horizontal"android:orientation="vertical"><Buttonandroid:layout_width="match_parent"android:layout_height="wrap_content"android:id="@+id/copy"android:text="复制"/><Buttonandroid:layout_width="match_parent"android:layout_height="wrap_content"android:id="@+id/paste"android:text="粘贴"/></LinearLayout><!--Tab页3--><LinearLayoutandroid:layout_width="match_parent"android:layout_height="match_parent"android:id="@+id/seek"android:gravity="center_horizontal"android:orientation="vertical"><EditTextandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:id="@+id/edit1"android:text="请输入检索关键字…"android:textSize="18sp"/><Buttonandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:id="@+id/seekbut1"android:text="搜索"/></LinearLayout><LinearLayoutandroid:layout_width="match_parent"android:layout_height="match_parent"android:id="@+id/time"android:gravity="center_horizontal"android:orientation="vertical"><TimePickerandroid: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; //定义TabHostprivate int[]layRes={R.id.file,R.id.edit,R.id.seek,R.id.time};//定义内嵌布局管理器IDprivate Button openButton,save,saveAs,copy,paste,seekbut1;@Overrideprotected 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("文件"); //定义TabSpecmyTab1.setIndicator("文件"); //设置选项文字myTab1.setContent(layRes[0]);  //设置显示的组件mytabHost.addTab(myTab1); //增加选项TabHost.TabSpec myTab2=mytabHost.newTabSpec("编辑");//定义TabSpecmyTab2.setIndicator("编辑").setContent(layRes[1]); //设置显示的组件mytabHost.addTab(myTab2);  //增加选项TabHost.TabSpec myTab3=mytabHost.newTabSpec("查看");//定义TabSpecmyTab3.setIndicator("查看").setContent(layRes[2]); //设置显示的组件mytabHost.addTab(myTab3);  //增加选项TabHost.TabSpec myTab4=mytabHost.newTabSpec("设置");//定义TabSpecmyTab4.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"><!--实现底部导航--><LinearLayoutandroid:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"><FrameLayoutandroid:id="@android:id/tabcontent"android:layout_width="match_parent"android:layout_height="0dp"android:layout_weight="1"><LinearLayoutandroid:id="@+id/file"android:layout_width="match_parent"android:layout_height="match_parent"android:gravity="center_horizontal"android:orientation="vertical"><Buttonandroid:id="@+id/open"android:layout_width="150dp"android:layout_height="wrap_content"android:text="打开文件" /><Buttonandroid:id="@+id/save"android:layout_width="150dp"android:layout_height="wrap_content"android:text="保存文件" /><Buttonandroid:id="@+id/saveAs"android:layout_width="150dp"android:layout_height="wrap_content"android:text="文件另存为" /></LinearLayout><LinearLayoutandroid:id="@+id/edit"android:layout_width="match_parent"android:layout_height="match_parent"android:gravity="center_horizontal"android:orientation="vertical"><Buttonandroid:id="@+id/copy"android:layout_width="match_parent"android:layout_height="wrap_content"android:text="复制" /><Buttonandroid:id="@+id/paste"android:layout_width="match_parent"android:layout_height="wrap_content"android:text="粘贴" /></LinearLayout><LinearLayoutandroid:id="@+id/seek"android:layout_width="match_parent"android:layout_height="match_parent"android:gravity="center_horizontal"android:orientation="vertical"><EditTextandroid:id="@+id/edit1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="请输入检索关键字..."android:textSize="18sp" /><Buttonandroid:id="@+id/seekbut1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="搜索" /></LinearLayout><LinearLayoutandroid:id="@+id/time"android:layout_width="match_parent"android:layout_height="match_parent"android:gravity="center_horizontal"android:orientation="vertical"><TimePickerandroid:id="@+id/seekbut"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="设置时间" /></LinearLayout></FrameLayout><TabWidgetandroid:id="@android:id/tabs"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_alignParentTop="true"android:layout_weight="0" /></LinearLayout>
</TabHost>

在这里插入图片描述


http://chatgpt.dhexx.cn/article/SbCcwEUs.shtml

相关文章

交叉熵:计算交叉熵损失函数nn.CrossEntropyLoss()

首先要提出的问题是。。。什么是损失函数&#xff1f;干什么的&#xff08;功能&#xff09;&#xff1f;类型有哪些&#xff1f; 1.什么是损失函数&#xff1f; 损失函数&#xff08;loss function&#xff09;或 代价函数&#xff08;cost function&#xff09;是将随机事件或…

softmax函数与交叉熵损失函数

本文主要介绍了当前机器学习模型中广泛应用的交叉熵损失函数与softmax激励函数。 这个损失函数主要应用于多分类问题&#xff0c;用于衡量预测值与实际值之间的相似程度。 交叉熵损失函数定义如下: L C E ( y ^ , y ∗ ) − ∑ i 1 N c l a s s e s y i ∗ l o g ( y i ^ …

【交叉熵损失函数】关于交叉熵损失函数的一些理解

目录 0. 前言1.损失函数&#xff08;Loss Function&#xff09;1.1 损失项1.2 正则化项 2. 交叉熵损失函数2.1 softmax2.2 交叉熵 0. 前言 有段时间没写博客了&#xff0c;前段时间主要是在精读一些计算机视觉的论文&#xff08;比如yolov1&#xff09;&#xff0c;以及学cs23…

CrossEntropy交叉熵损失函数及softmax函数的理解

参考链接1 参考链接2 参考链接3 参考链接4 &#xff08;一&#xff09;什么是Sigmoid函数和softmax函数&#xff1f; 提到二分类问题容易想到逻辑回归算法&#xff0c;而逻辑回归算法最关键的步骤就是将线性模型输出的实数域映射到[0, 1]表示概率分布的有效实数空间&#xff…

【基础篇】交叉熵损失函数(Cross Entropy Loss)

文章目录 1. 理论知识2. 代码 1. 理论知识 我们需要关注那些按常理来说不太可能发生的事情。『信息量』就是用来度量事件的不确定性&#xff0c; 事件包含的信息量应与其发生的概率负相关 。假设 X X X是一个离散型随机变量&#xff0c;它的取值集合为 { x 1 , x 2 , . . . ,…

损失函数-交叉熵的推导和二分类交叉熵

交叉熵 期望&#xff1a; 期望就是所有随机变量的均值。 E(X)X1*P&#xff08;X1&#xff09;X2*P&#xff08;X2&#xff09;X3*P&#xff08;X3&#xff09; 熵&#xff1a; 熵表示所有信息量的期望。 信息量如何计算呢&#xff1f; 概率值取Log&#xff0c;然后加个负…

深度学习中的损失函数(交叉熵)

0、前景介绍 对于线性回归模型适用于输出为连续值的情景&#xff0c;但是在模型输出是一个像图像类别这样的离散值时。对于这样离散值的预测问题&#xff0c;通常使用一些例如sigmoid/softmax的分类模型。 1. 图像分类任务 假设下面两个模型都是通过softmax的方式得到对于每…

图示Softmax及交叉熵损失函数

Softmax函数 Softmax是将神经网络得到的多个值&#xff0c;进行归一化处理&#xff0c;使得到的值在之间&#xff0c;让结果变得可解释。即可以将结果看作是概率&#xff0c;某个类别概率越大&#xff0c;将样本归为该类别的可能性也就越高。Softmax就如下图&#xff08;借鉴李…

最全的交叉熵损失函数(Pytorch)

损失函数 引言BCELossBCEWithLogitsLossNLLLossCrossEntropyLoss总结参考 引言 这里主要讲述pytorch中的几种交叉熵损失类&#xff0c;熵是用来描述一个系统的混乱程度,通过交叉熵我们就能够确定预测数据与真是数据之间的相近程度。交叉熵越小&#xff0c;表示数据越接近真实样…

简单的交叉熵损失函数,你真的懂了吗?

个人网站&#xff1a;红色石头的机器学习之路 CSDN博客&#xff1a;红色石头的专栏 知乎&#xff1a;红色石头 微博&#xff1a;RedstoneWill的微博 GitHub&#xff1a;RedstoneWill的GitHub 微信公众号&#xff1a;AI有道&#xff08;ID&#xff1a;redstonewill&#xf…

交叉熵损失概念

交叉熵是信息论中的一个概念&#xff0c;要想了解交叉熵的本质&#xff0c;需要先从最基本的概念讲起。 1. 信息量 首先是信息量。假设我们听到了两件事&#xff0c;分别如下&#xff1a; 事件A&#xff1a;巴西队进入了2018世界杯决赛圈。 事件B&#xff1a;中国队进入了2…

softmax交叉熵损失函数深入理解(二)

0、前言 前期博文提到经过两步smooth化之后&#xff0c;我们将一个难以收敛的函数逐步改造成了softmax交叉熵损失函数&#xff0c;解决了原始的目标函数难以优化的问题。Softmax 交叉熵损失函数是目前最常用的分类损失函数&#xff0c;本博文继续学习Softmax 交叉熵损失函数的改…

史上最全交叉熵损失函数详解

在我们自学神经网络神经网络的损失函数的时候会发现有一个思路就是交叉熵损失函数&#xff0c;交叉熵的概念源于信息论&#xff0c;一般用来求目标与预测值之间的差距。比如说我们在人脑中有一个模型&#xff0c;在神经网络中还有一个模型&#xff0c;我们需要找到神经网络模型…

交叉熵损失函数原理详解

交叉熵损失函数原理详解 之前在代码中经常看见交叉熵损失函数(CrossEntropy Loss)&#xff0c;只知道它是分类问题中经常使用的一种损失函数&#xff0c;对于其内部的原理总是模模糊糊&#xff0c;而且一般使用交叉熵作为损失函数时&#xff0c;在模型的输出层总会接一个softm…

损失函数——交叉熵损失函数

一篇弄懂交叉熵损失函数 一、定义二、交叉熵损失函数&#xff1a;知识准备&#xff1a;1、信息熵&#xff1a;将熵引入到信息论中&#xff0c;命名为“信息熵”2、 KL散度&#xff08;相对熵&#xff09;&#xff1a; 交叉熵&#xff1a;结论&#xff1a; Softmax公式Sigmoid常…

交叉熵损失函数详解

我们知道&#xff0c;在二分类问题模型&#xff1a;例如逻辑回归「Logistic Regression」、神经网络「Neural Network」等&#xff0c;真实样本的标签为 [0&#xff0c;1]&#xff0c;分别表示负类和正类。模型的最后通常会经过一个 Sigmoid 函数&#xff0c;输出一个概率值&am…

交叉熵损失函数(CrossEntropy Loss)(原理详解)

监督学习主要分为两类&#xff1a; 分类&#xff1a;目标变量是离散的&#xff0c;如判断一个西瓜是好瓜还是坏瓜&#xff0c;那么目标变量只能是1&#xff08;好瓜&#xff09;,0&#xff08;坏瓜&#xff09;回归&#xff1a;目标变量是连续的&#xff0c;如预测西瓜的含糖率…

nn.CrossEntropyLoss()交叉熵损失函数

1、nn.CrossEntropyLoss() 在pytorch中nn.CrossEntropyLoss()为交叉熵损失函数&#xff0c;用于解决多分类问题&#xff0c;也可用于解决二分类问题。在使用nn.CrossEntropyLoss()其内部会自动加上Sofrmax层 nn.CrossEntropyLoss()的计算公式如下&#xff1a; 其中&#xff0c…

损失函数——交叉熵损失函数(CrossEntropy Loss)

损失函数——交叉熵损失函数&#xff08;CrossEntropy Loss&#xff09; 交叉熵函数为在处理分类问题中常用的一种损失函数&#xff0c;其具体公式为&#xff1a; 1.交叉熵损失函数由来 交叉熵是信息论中的一个重要概念&#xff0c;主要用于度量两个概率分布间的差异性。首先…

损失函数——交叉熵损失(Cross-entropy loss)

交叉熵损失&#xff08;Cross-entropy loss&#xff09;是深度学习中常用的一种损失函数&#xff0c;通常用于分类问题。它衡量了模型预测结果与实际结果之间的差距&#xff0c;是优化模型参数的关键指标之一。以下是交叉熵损失的详细介绍。 假设我们有一个分类问题&#xff0…