iis服务器助手广告广告
返回顶部
首页 > 资讯 > 移动开发 >Android组件必学之TabHost使用方法详解
  • 898
分享到

Android组件必学之TabHost使用方法详解

方法tabhostAndroid 2022-06-06 08:06:35 898人浏览 独家记忆
摘要

一、TabHost用法 通常情况下我们会通过继承TabActivity,调用getTabHost()获取TabHost实例,下面是具体过程。 TabHostActivity.j

一、TabHost用法
通常情况下我们会通过继承TabActivity,调用getTabHost()获取TabHost实例,下面是具体过程。
TabHostActivity.java


public class TabHostActivity extends TabActivity {
 private TabHost tabHost;
 private Intent certificateIntent;
 private Intent feeIntent;
 private Intent scoreIntent;
 private Intent studyIntent;
 private Intent moreIntent;
 @Override
 publicvoid onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  tabHost = getTabHost();
  initIntent();
  addSpec();
 }
 
 privatevoid initIntent() {
  studyIntent = new Intent(this, StudyActivity.class);
  scoreIntent = new Intent(this, ScoReactivity.class);
  feeIntent = new Intent(this, FeeActivity.class);
  certificateIntent = new Intent(this, CertificateActivity.class);
  moreIntent = new Intent(this, MoreActivity.class);
 }
 
 privatevoid addSpec() {
  tabHost.addTab(this.buildTagSpec("tab_study",
R.string.study_progress,R.drawable.account01, studyIntent));
 tabHost.addTab(this.buildTagSpec("tab_score",
R.string.test_score,R.drawable.account02, scoreIntent));
  tabHost.addTab(this.buildTagSpec("tab_fee",
R.string.fee_pay,R.drawable.account03, feeIntent));
  tabHost.addTab(this.buildTagSpec("tab_certificate", R.string.certificate_grant,R.drawable.accountcertificateIntent));
  tabHost.addTab(this.buildTagSpec("tab_more", R.string.more,
    R.drawable.account05, moreIntent));
 }
 
 private TabHost.TabSpec buildTagSpec(String tagName, int tagLable,
   int icon, Intent content) {
  returntabHost
    .newTabSpec(tagName)
    .setIndicator(getResources().getString(tagLable),
      getResources().getDrawable(icon)).setContent(content);
 }}

运行结果如下图所示

 

我们发现标签位置处于界面上方,但是我们看到的很多应用的标签都处于界面底部。
如下图所示

 

我们要实现这种效果只需要将TabActivity的默认布局覆盖即可。新布局只需将标签和标签对应内容的相对位置调换一下就可以了,这里是用相对布局将标签对应内容的位置放到了标签的上方。不要改动id(会抛异常,提示必须要用指定的id)。不要忘了在onCreate()里设置新布局将TabActivity的默认布局覆盖。


 @Override
 publicvoid onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
setContentView(R.layout.tab);
  tabHost = getTabHost();
  initIntent();
  addSpec();
 }

tab.xml


<?xml version="1.0" encoding="UTF-8"?>
<!-- TabHost组件id值不可变-->
<TabHostxmlns:Android=Http://schemas.android.com/apk/res/android
 android:id="@android:id/tabhost"
 android:layout_height="fill_parent"
 android:layout_width="fill_parent">
 <RelativeLayout android:orientation="vertical"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent">
  <!-- TabWidget组件id值不可变-->
  <TabWidget android:id="@android:id/tabs"
   android:layout_width="fill_parent"
   android:layout_height="wrap_content"
   android:layout_alignParentBottom="true">
  </TabWidget>
  <!-- FrameLayout布局,id值不可变-->
  <FrameLayout android:id="@android:id/tabcontent"
   android:layout_width="fill_parent"
   android:layout_height="fill_parent"
   android:layout_above="@android:id/tabs">
  </FrameLayout>
 </RelativeLayout>
</TabHost>

 通常在项目中我们都会有一个自定义的Activity基类,我们会让所有的界面Activity去继承这个基类。但是要使用TabHost就要继承TabActivity,所以我们可以定义两个基类,一个是普通Activity界面的基类,另一个是包含TabHost界面的基类,让这个基类继承TabActivity即可。
二、TabHost用法—定义Tab标签样式
第一节“TabHost用法”中我们介绍了通过TabHost实现标签页效果。但是在实际项目中我们可能更希望定义自己的Tab标签样式使界面效果更佳。既然不能改变系统的Tab样式,那么我们可以选择隐藏系统的东西,使用自己定义的东西(这种方式很好用,以后会详细介绍)。反编译新浪微博的项目后会发现,他们在布局中隐藏了TabWidget即Tab标签而使用一组RadioButton来代替。既然是自己定义的,那肯定是可以自己决定显示样式了,那我们的问题也就解决了。

这里我使用的是“TabHost用法—两种实现方式”一文种提到的第二种实现方式,继承Activity来使用TabHost的。先把代码贴上来(红色字体部分为修改或添加的代码)。
TabHostActivity.java


public class TabHostActivity extends Activity implements
  OnCheckedChangeListener {
 private TabHost tabHost;
 private Intent certificateIntent;
 private Intent feeIntent;
 private Intent scoreIntent;
 private Intent studyIntent;
 private Intent moreIntent;
 @Override
 publicvoid onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.tab);
  // tabHost = getTabHost();
  tabHost = (TabHost) findViewById(R.id.my_tabhost);
  LocalActivityManager groupActivity =
new LocalActivityManager(this,false);
  groupActivity.dispatchCreate(savedInstanceState);
  tabHost.setup(groupActivity);
  initIntent();
  addSpec();
  ((RadioGroup) findViewById(R.id.tab_radiogroup))
    .setOnCheckedChangeListener(this);
 }
 
 privatevoid initIntent() {
  studyIntent = new Intent(this, StudyActivity.class);
  scoreIntent = new Intent(this, ScoreActivity.class);
  feeIntent = new Intent(this, FeeActivity.class);
  certificateIntent = new Intent(this, CertificateActivity.class);
  moreIntent = new Intent(this, MoreActivity.class);
 }
 
 privatevoid addSpec() {
tabHost.addTab(this.buildTagSpec("tab_study", R.string.study_progress,
    R.drawable.account01, studyIntent));
tabHost.addTab(this.buildTagSpec("tab_score", R.string.test_score,
    R.drawable.account02, scoreIntent));
  tabHost.addTab(this.buildTagSpec("tab_fee", R.string.fee_pay,
    R.drawable.account03, feeIntent));
  tabHost.addTab(this.buildTagSpec("tab_certificate",
    R.string.certificate_grant, R.drawable.account04,
    certificateIntent));
  tabHost.addTab(this.buildTagSpec("tab_more", R.string.more,
    R.drawable.account05, moreIntent));
 }
 
 private TabHost.TabSpec buildTagSpec(String tagName, int tagLable,
   int icon, Intent content) {
  returntabHost
    .newTabSpec(tagName)
    .setIndicator(getResources().getString(tagLable),
      getResources().getDrawable(icon)).setContent(content);
 }
 @Override
 public void onCheckedChanged(RadioGroup group, int checkedId) {
  switch (checkedId) {
  case R.id.radio_button_study:
   tabHost.setCurrentTabByTag("tab_study");
   break;
  case R.id.radio_button_score:
   tabHost.setCurrentTabByTag("tab_score");
   break;
  case R.id.radio_button_certificate:
   tabHost.setCurrentTabByTag("tab_certificate");
   break;
  case R.id.radio_button_fee:
   tabHost.setCurrentTabByTag("tab_fee");
   break;
  case R.id.radio_button_more:
   tabHost.setCurrentTabByTag("tab_more");
   break;
  }
 }
}

tab.xml


<?xml version="1.0" encoding="UTF-8"?>
<TabHost android:id="@+id/my_tabhost" android:layout_width="fill_parent"
   android:layout_height="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android">
 <LinearLayout android:orientation="vertical"
android:layout_width="fill_parent" android:layout_height="fill_parent">
  <FrameLayout android:id="@android:id/tabcontent"
     android:layout_width="fill_parent" android:layout_height="0.0dip"
      android:layout_weight="1.0" />
<TabWidget android:id="@android:id/tabs" android:visibility="Gone"
android:layout_width="fill_parent" android:layout_height="wrap_content"
     android:layout_weight="0.0" />
  <RadioGroup android:id="@+id/tab_radiogroup"
    android:background="@drawable/tabs_bg" android:layout_width="fill_parent"
   android:layout_height="wrap_content" android:gravity="center_vertical"
   android:layout_gravity="bottom" android:orientation="horizontal">
   <RadioButton android:id="@+id/radio_button_study"
    android:layout_marginTop="2.0dip" android:text="学习进度"
    android:drawableTop="@drawable/account01" style="@style/tab_button_bottom"
    android:checked="true" />
   <RadioButton android:id="@+id/radio_button_score"
    android:layout_marginTop="2.0dip" android:text="考试成绩"
    android:drawableTop="@drawable/account02" style="@style/tab_button_bottom" />
   <RadioButton android:id="@+id/radio_button_certificate"
    android:layout_marginTop="2.0dip" android:text="证书发放"
    android:drawableTop="@drawable/account03" style="@style/tab_button_bottom" />
   <RadioButton android:id="@+id/radio_button_fee"
    android:layout_marginTop="2.0dip" android:text="费用缴纳"
    android:drawableTop="@drawable/account04" style="@style/tab_button_bottom" />
   <RadioButton android:id="@+id/radio_button_more"
    android:layout_marginTop="2.0dip" android:text="更多"
    android:drawableTop="@drawable/account05" style="@style/tab_button_bottom" />
  </RadioGroup>
 </LinearLayout>
</TabHost>

styles.xml


<?xml version="1.0" encoding="utf-8"?>
<resources>
 <!-- TabHost标签按钮样式 -->
 <style name="tab_button_bottom">
  <item name="android:textSize">12px</item>
  <item name="android:textColor">#ffffffff</item>
  <item name="android:ellipsize">marquee</item>
  <item name="android:gravity">center_horizontal</item>
  <item name="android:background">@drawable/tab_btn_bg</item>
  <item name="android:layout_marginTop">2.0dip</item>
  <item name="android:button">@null</item>
  <item name="android:paddingTop">6dip</item>
  <item name="android:drawablePadding">4dip</item>
  <item name="android:layout_width">fill_parent</item>
  <item name="android:layout_height">wrap_content</item>
  <item name="android:layout_weight">1.0</item>
  <item name="android:singleLine">true</item>
 </style>
 <!-- 页面标题LinearLayout样式 -->
 <style name="activity_title_background">
 <item name="android:background">@drawable/title_background</item>
  <item name="android:layout_width">fill_parent</item>
  <item name="android:layout_height">wrap_content</item>
  <item name="android:layout_alignParentTop">true</item>
  <item name="android:gravity">center</item>
 </style>
 <!-- 界面标题TextView样式 -->
 <style name="activity_title_text">
  <item name="android:textSize">14dip</item>
  <item name="android:textColor">@drawable/white</item>
  <item name="android:layout_width">wrap_content</item>
  <item name="android:layout_height">wrap_content</item>
  <item name="android:gravity">center</item>
 </style>
</resources>

运行结果如下图所示


程序重要部分:
1.  红色字体部分。
2.  布局文件tab.xml,可以看到该布局文件中将TabWidget隐藏(android:visibility="gone")而以一个RadioGroup取而代之。
3.  为RadioGroup设置OnCheckedChangeListener监听,通过onCheckedChanged方法对各个RadioButton点击事件的处理完成标签切换。 

其实我当初考虑过为什么要用RadioButton而不用普通的Button。后来通过自己做项目,发现使用RadioGroup有以下优点
1.另外就是布局上比较方便易懂,不用再去用LinearLayout等布局去包含Button。
2. 我们可以很方便的获得当前选中的标签,当然通过TabHost的tabHost.getCurrentTabTag()和getCurrentTab()也是可以的。
3.设置监听很方便,只需要为RadioGroup设置监听就行了,程序中对应的代码是


((RadioGroup) findViewById(R.id.tab_radiogroup))
       .setOnCheckedChangeListener(this);

   如果用Button的话我们需要为所有的Button一个一个去设置监听,相对来说比较麻烦。
4.  或许最重要的一点是因为RadioButton本身就支持图片和文字的上下布局,只需指定图片和文字是什么就可以了而不需要我们自己去实现这种布局。


<RadioButton android:id="@+id/radio_button_more"
   android:layout_marginTop="2.0dip"
android:text="更多"
   android:drawableTop="@drawable/account05"
   style="@style/tab_button_bottom" />

当然如果如果RadioButton不能满足我们的项目需求,比如我们不需要图片又不想让文字靠底部显示,而是居中显示,这时我们就可以用其他组件代替RadioButton。其实我们可以通过修改或自定义等方式实现多种漂亮的效果,比如“人人网”手机客户端的个人主页中Tab标签是可以左右滑动的。

原创作者:男人应似海

您可能感兴趣的文章:Android组件TabHost实现页面中多个选项卡切换效果android TabHost(选项卡)的使用方法android 选项卡(TabHost)如何放置在屏幕的底部Android控件之TabHost用法实例分析Android 使用FragmentTabhost代替Tabhost详解Android应用中使用TabHost组件进行布局的基本方法Android编程实现设置TabHost当中字体的方法详解Android TabHost的多种实现方法 附源码下载Android Tabhost使用方法详解Android TabHost组件使用方法详解Android TabHost选项卡标签图标始终不出现的解决方法


--结束END--

本文标题: Android组件必学之TabHost使用方法详解

本文链接: https://www.lsjlt.com/news/24955.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • Android学习之Span的使用方法详解
    目录Span集合段落类Span其他Span展示效果小试牛刀小结Span集合 段落类Span BulletSpan 为段落开头增加项目符号并支持大小、颜色、弧度 span.append...
    99+
    2024-04-02
  • android选项卡TabHost功能用法详解
    本文实例为大家分享了android选项卡TabHost功能用法,供大家参考,具体内容如下 首先定义三个xml文件,分别为l1.xml,l2.xml,l3.xml,每个选项卡的标签页的...
    99+
    2024-04-02
  • Android学习之BottomSheetDialog组件的使用
    目录基本介绍基础使用其他功能实现圆角样式实现去弹窗外部遮罩阴影关闭触发设置列表视图使用基本介绍 BottomSheetDialog是底部操作控件,可在屏幕底部创建一个支持滑动关闭视图...
    99+
    2024-04-02
  • Flutter滚动组件之ListView使用方法详解
    ListView ListView是最常用的可滚动组件之一,它可以沿一个方向线性排布所有子组件,并且它也支持基于Sliver的延迟构建模型。我们看看ListView的默认构造函数定义...
    99+
    2024-04-02
  • Android控件AppWidgetProvider使用方法详解
    介绍AppWidgetProvider是Android中提供的用于实现桌面小工具的类,其本质是一个广播,即BroadcastReceiver,在实际的使用中,把AppWidgetProvider当成一个BroadcastReceiver即可...
    99+
    2023-05-30
    android appwidgetprovider pp
  • android时间选择控件之TimePickerView使用方法详解
    相信大家都有这样的一个需求,选择相应开始时间和结束时间,对数据进行筛选,下面就将使用TimePickerView实现这么一个功能。 一、先导入依赖 implementation "c...
    99+
    2024-04-02
  • vue之ele多级联组件的使用方法详解
    本文实例为大家分享了vue之ele多级联组件的使用具体代码,供大家参考,具体内容如下 多级联组件的使用 html <el-cascader         ref="casc...
    99+
    2024-04-02
  • Android组件之服务的详解
    目录一、服务的概念二、Android的多线程编程2.1 线程的基本用法2.2 在子线程中更新UI更新方式一更新方式二2.3 解析异步消息处理机制2.4 使用AsyncTask三、服务...
    99+
    2024-04-02
  • Android四大组件之BroadcastReceiver详解
    BroadcastReceiver是Android四大组件之一,用于接收和处理系统广播或者应用内发送的广播。广播是一种跨组件、跨应用的通信机制,可以用于在应用内部或者应用之间传递消息或者事件。BroadcastReceiver的主要作用...
    99+
    2023-08-09
    Android
  • jsp学习之scriptlet的使用方法详解
    在JSP中,scriptlet是一种在JSP页面中嵌入Java代码的方式。它被包含在标签中,可以在其中编写任意的Java代码。下面是...
    99+
    2023-08-11
    jsp
  • aspjpeg组件使用方法284455详解
    1、什么是AspJpeg? AspJpeg是一款功能强大的基于Microsoft IIS环境的图片处理组件,网络上对其进行详细和深入介绍的中文文章并不多,即使有一般也只是牵涉到图片缩...
    99+
    2023-05-20
    aspjpeg组件使用方法
  • aspjpeg组件使用方法284435详解
    1、什么是AspJpeg? AspJpeg是一款功能强大的基于Microsoft IIS环境的图片处理组件,网络上对其进行详细和深入介绍的中文文章并不多,即使有一般也只是牵涉到图片缩...
    99+
    2023-05-20
    aspjpeg组件使用方法
  • aspjpeg组件使用方法284627详解
    1、什么是AspJpeg? AspJpeg是一款功能强大的基于Microsoft IIS环境的图片处理组件,网络上对其进行详细和深入介绍的中文文章并不多,即使有一般也只是牵涉到图片缩...
    99+
    2023-05-20
    aspjpeg组件使用方法
  • Android学习之菜单的使用方法
    本文实例为大家分享了Android学习之菜单使用的具体代码,供大家参考,具体内容如下 Android中菜单包含上下文菜单和选项菜单两种类型。 使用统一的菜单类来管理菜单: Menu、...
    99+
    2024-04-02
  • AndroidJetpack组件之ViewModel使用详解
    目录ViewModel的诞生ViewModel的作用ViewModel的简单应用ViewModel的诞生 解决以下几个问题: 瞬态数据丢失异步调用的内存泄漏 当我们取以异步操作区网络...
    99+
    2023-05-14
    Android Jetpack ViewModel Android ViewModel
  • Python学习之元组的使用详解
    目录元组的创建元组的删除元组部分元素的输出元组的嵌套元组元素的个数计算:len()元组中的元素最大最小值的求解:max(),min()元组中某元素出现的次数:count函数元组中某元...
    99+
    2024-04-02
  • Android开发之MediaPlayer基本使用方法详解
    本文实例讲述了Android MediaPlayer基本使用方法。分享给大家供大家参考,具体如下:使用MediaPlayer播放音频或者视频的最简单例子:Java代码部分:public class MediaPlayerStudy exte...
    99+
    2023-05-31
    android mediaplayer roi
  • Vue组件化学习之scoped详解
    目录简介总结简介 主要介绍scoped的作用。 先弄一个案例: main.js: //引入vue依赖 import Vue from 'vue' //引入组件App import ...
    99+
    2024-04-02
  • JavaScript分页组件使用方法详解
    分页组件是web开发中常见的组件,请完成pagination函数,在id为jsPagination的DOM元素中完成分页的显示部分,需求如下 1、最多连续显示5页,居中高亮显示cur...
    99+
    2024-04-02
  • Flutter投票组件使用方法详解
    本文实例为大家分享了Flutter投票组件的使用方法,供大家参考,具体内容如下 前景 基于公司项目需求,仿照微博实现投票功能。 开发遇到的问题 1.选项列表的高度,自适应的问题;2....
    99+
    2024-04-02
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作