TabHost详解0

article/2025/9/9 14:57:03

一、TabHost是一个选项卡容器,通过标签页将多个Activity整合到一起。

TabHost的三要素为:TabWidget、FrameLayout、List<TabSpec>。

其主要的使用方式有两种: 1.继承TabActivity,结合对应的xml配置文件导入tab选项内容体 

 2.继承Activity,结合拥有TabHost标签的xml配置文件导入

第一种方式:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="fill_parent"android:layout_height="fill_parent"android:orientation="vertical" ><LinearLayoutandroid:id="@+id/tab1"android:layout_width="fill_parent"android:layout_height="fill_parent"android:orientation="vertical" ><TextViewandroid:id="@+id/view1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="TabHost 1" /></LinearLayout><LinearLayoutandroid:id="@+id/tab2"android:layout_width="fill_parent"android:layout_height="fill_parent" ><TextViewandroid:id="@+id/view2"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="TabHost 2" /></LinearLayout><LinearLayoutandroid:id="@+id/tab3"android:layout_width="fill_parent"android:layout_height="fill_parent"android:orientation="vertical" ><TextViewandroid:id="@+id/view3"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="TabHost 3" /></LinearLayout><LinearLayoutandroid:id="@+id/tab4"android:layout_width="fill_parent"android:layout_height="fill_parent"android:orientation="vertical" ><TextViewandroid:id="@+id/view4"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="TabHost 4" /></LinearLayout></LinearLayout>
public class MainActivity extends TabActivity {@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);//获得TabHost对象TabHost tb=this.getTabHost();LayoutInflater.from(this).inflate(R.layout.activity_main, tb.getTabContentView(), true);//setIndicator方法设置选项卡的标题和对应图标  setContent设置对应的内容tb.addTab(tb.newTabSpec("spec1").setIndicator("选项卡1",getResources().getDrawable(R.drawable.audio)).setContent(R.id.tab1));tb.addTab(tb.newTabSpec("spec2").setIndicator("选项卡2",getResources().getDrawable(R.drawable.folder)).setContent(R.id.tab2));tb.addTab(tb.newTabSpec("spec3").setIndicator("选项卡3",getResources().getDrawable(R.drawable.image)).setContent(R.id.tab3));}
}


第二种方式:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="fill_parent"android:layout_height="fill_parent"android:orientation="vertical" ><TabHostandroid:id="@+id/tabhost"android:layout_width="match_parent"android:layout_height="match_parent" ><LinearLayoutandroid:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical" ><TabWidgetandroid:id="@android:id/tabs"android:layout_width="match_parent"android:layout_height="wrap_content"></TabWidget><FrameLayoutandroid:id="@android:id/tabcontent"android:layout_width="match_parent"android:layout_height="match_parent" ><LinearLayoutandroid:id="@+id/tab1"android:layout_width="match_parent"android:layout_height="match_parent" ></LinearLayout><LinearLayoutandroid:id="@+id/tab2"android:layout_width="match_parent"android:layout_height="match_parent" ></LinearLayout><LinearLayoutandroid:id="@+id/tab3"android:layout_width="match_parent"android:layout_height="match_parent" ></LinearLayout></FrameLayout></LinearLayout></TabHost></LinearLayout>
其中 TabWidget、FrameLayout的名字必须是固定的。

public class MainActivity extends Activity {@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);TabHost tb=(TabHost)findViewById(R.id.tabhost);tb.setup();TabHost.TabSpec spec1=tb.newTabSpec("tab1");spec1.setContent(R.id.tab1);spec1.setIndicator("选项卡一");tb.addTab(spec1);TabHost.TabSpec spec2=tb.newTabSpec("tab2");spec2.setContent(R.id.tab2);spec2.setIndicator("选项卡二");tb.addTab(spec2);TabHost.TabSpec spec3=tb.newTabSpec("tab3");spec3.setContent(R.id.tab3);spec3.setIndicator("选项卡三");tb.addTab(spec3);}
}

Tabhost中的常用方法:

getTag():获取标签字符串

getCurrentTab():获取当前选项卡ID

getTabWidget():根据系统规定的id:tabs来找到Tabwidget

setOnTabChangedListener():change事件




http://chatgpt.dhexx.cn/article/8FGFYIei.shtml

相关文章

TabHost

TabHost的实现有两种方式&#xff0c;第一种继承TabActivity&#xff0c;从TabActivity中用getTabHost()方法获取TabHost。各个Tab中的内容在布局文件中定义就行了。 mainActivity.xml private TabHost myTabHost;Overridepublic void onCreate(Bundle savedInstanceState) {su…

Android初级控件TabHost

TabHost我们都知道是用来实现导航栏布局来切换页面的&#xff0c;这个也是元老级的控件了&#xff0c;现在逐渐被TabLayout,BottomNavigationBar,使用RadioButton自定义。。。等等给取代了。TabHost有个好处就是它添加的是Activity而不像上面那些全部使用Fragment来显示内容。 …

Android中TabHost嵌套TabHost

在嵌套TabHost时&#xff0c;先后遇到了以下情况&#xff1a; 问题1&#xff1a;内部TabHos无显示&#xff0c;只显示了其中的一个Activity&#xff1b; 解决&#xff1a;按下文比对主子TabHos的布局文件和java文件并修改&#xff1b; 问题2&#xff1a;如上所做后&#xff…

Android的Tab与TabHost讲解

在Android应用中&#xff0c;经常会用到TabHost选项卡,可以方便地在不同页面间切换。之前看过网上的一些教程&#xff0c;但大多都是一个形式&#xff0c;看得迷迷糊糊&#xff0c;不能让人很好的理解和学习。所以&#xff0c;在此详细地列出了Tab与TabHost的使用方法&#xff…

Android - TabHost 选项卡功能用法详解

一、实例 二.、TabHost介绍 TabHost组件可以在界面中存放多个选项卡, 很多软件都使用了改组件进行设计; 1. TabHost常用组件 TabWidget : 该组件就是TabHost标签页中上部 或者 下部的按钮, 可以点击按钮切换选项卡; TabSpec : 代表了选项卡界面, 添加一个TabSpec即可添加到TabH…

【转】TabHost详解

请大家尊重作者版权&#xff0c;转载请标明出处&#xff1a;http://blog.csdn.net/harvic880925/article/details/17120325 前言&#xff1a;今天仔细研究了下TabHost&#xff0c;主要是为了实现微信底部导航栏的功能&#xff0c;最后也给出一个链接&#xff0c;这位老兄用TabH…

Android入门第十一篇之TabHost,TabWidget

本文来自http://blog.csdn.net/hellogv/ &#xff0c;引用必须注明出处&#xff01; 这回要介绍的是Android的Tab控件&#xff0c;Tab控件可以达到分页的效果&#xff0c;让一个屏幕的内容尽量丰富&#xff0c;当然也会增加开发的复杂程度&#xff0c;在有必要的时候再使用。An…

android Tabhost部件

本文结合源代码和实例来说明TabHost的用法。 使用TabHost 可以在一个屏幕间进行不同版面的切换&#xff0c;例如android自带的拨号应用&#xff0c;截图&#xff1a; 查看tabhost的源代码&#xff0c;主要实例变量有&#xff1a; private TabWidget mTabWidget; …

TabHost的用法

http://blog.csdn.net/lastsweetop/article/details/5566200 本文结合源代码和实例来说明TabHost的用法。 使用TabHost 可以在一个屏幕间进行不同版面的切换&#xff0c;例如android自带的拨号应用&#xff0c;截图&#xff1a; 查看tabhost的源代码&#xff0…

ViewPager和Tabhost结合,可滑动的tabhost

有朋友反映资源下载下来有问题&#xff0c;我看了下&#xff0c;确实是&#xff0c;已更新下面文章中的代码和资源&#xff0c;现在可以好好的跑起来了&#xff0c;另外还改动了个小地方的逻辑&#xff0c;因为我在使用中出了点小错&#xff0c;需要的可以试下。另外&#xff0…

TabSpec和TabHost实例

TabSpec与TabHost TabHost相当于浏览器中浏览器分布的集合&#xff0c;而Tabspec则相当于浏览器中的每一个分页面。d在Android中&#xff0c;每一个TabSpec分布可以是一个组件&#xff0c;也可以是一个布局&#xff0c;然后将每一个分页装入TabHost中&#xff0c;TabHost即可将…

Android中的TabHost

介绍 有时&#xff0c;我们想在一个window中显示多个视图&#xff0c;这时就需要用到Tab容器。在Android里它叫TabHost。 使用TabHost有两种方式&#xff1a; 在相同的activity中使用TabHost导航多个视图使用TabHost导航多个Activity(通过intents) Tab应用的结构 TabHost的A…

Android修行手册 - TabHost回忆

往期文章分享 点击跳转>《导航贴》- Unity手册&#xff0c;系统实战学习点击跳转>《导航贴》- Android手册&#xff0c;重温移动开发 &#x1f449;关于作者 众所周知&#xff0c;人生是一个漫长的流程&#xff0c;不断克服困难&#xff0c;不断反思前进的过程。在这个过…

Android TabHost的使用

1. 最简单的TabHost&#xff0c;Tab来自于layout下的元素 &#xff08;只从1个Layout中取数据&#xff09; &#xff08;1&#xff09;效果图 &#xff08;2&#xff09;代码 1&#xff09;tab_demo.xml <?xml version"1.0" encoding"utf-8"?&g…

TabHost详解

前言&#xff1a;今天仔细研究了下TabHost&#xff0c;主要是为了实现微信底部导航栏的功能&#xff0c;最后也给出一个链接&#xff0c;这位老兄用TabHost基本做出来了微信导航栏。 正文 TabHost的实现分为两种&#xff0c;一个是不继承TabActivity&#xff0c;一个是继承自…

Android选项卡TabHost功能和用法

1、选项卡TabHost介绍 TabHost可以方便地在窗口上放置多个标签页&#xff0c;每个标签页相当于获得了一个与外部容器大小相同的组件摆放区域 TabHost是一个简单的容器&#xff0c;提供如下两种方法来创建选项卡 newTabSpec(String tag):创建选项卡 addTab(TabHost.TabSpec tabS…

[Android] 选项卡组件TabHost

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

交叉熵:计算交叉熵损失函数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…