TabHost的两种使用方法

2024-05-29 10:38
文章标签 使用 方法 两种 tabhost

本文主要是介绍TabHost的两种使用方法,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

Android 实现tab视图有2种方法,一种就是继承tabactivity,一种是在布局页面中定义<tabhost>标签,

第一种:使用系统自带写好的TabHost(及继承自TabActivity类)

其具体步骤如下:
(1)使用setContentView()方法显示界面。
(2)TabHost对象获得并设置。
(3)创建并设置TabSpec对象。
(4)向TabHost中添加TabSpec完成标签页的使用。

<?xml version="1.0" encoding="utf-8"?>  
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"  android:layout_width="fill_parent"  android:layout_height="fill_parent">  <LinearLayout android:id="@+id/tab1"  android:layout_width="fill_parent" android:layout_height="fill_parent"  androidrientation="vertical">  <TextView android:id="@+id/TextView1"  android:text="This is a tab1" android:layout_width="fill_parent"  android:layout_height="wrap_content">  </TextView>  </LinearLayout>  <LinearLayout android:id="@+id/tab2"  android:layout_width="fill_parent" android:layout_height="fill_parent"  androidrientation="vertical">  <TextView android:id="@+id/TextView2"  android:text="This is a tab2" android:layout_width="fill_parent"  android:layout_height="wrap_content">  </TextView>  </LinearLayout>  <LinearLayout android:id="@+id/tab3"  android:layout_width="fill_parent" android:layout_height="fill_parent"  androidrientation="vertical">  <TextView android:id="@+id/TextView3"  android:text="This is a tab3" android:layout_width="fill_parent"  android:layout_height="wrap_content">  </TextView>  </LinearLayout>  
</FrameLayout> 


import android.app.AlertDialog;  
import android.app.Dialog;  
import android.app.TabActivity;  
import android.content.DialogInterface;  
import android.os.Bundle;  
import android.view.LayoutInflater;  
import android.widget.TabHost;  public class Test_TabWidget extends TabActivity {  /** Called when the activity is first created. */  private TabHost tabHost;  @Override  public void onCreate(Bundle savedInstanceState) {  super.onCreate(savedInstanceState);  // setContentView(R.layout.main);  tabHost = this.getTabHost();  LayoutInflater li = LayoutInflater.from(this);  li.inflate(R.layout.main, tabHost.getTabContentView(), true);  tabHost.addTab(tabHost.newTabSpec("Tab_1").setContent(R.id.tab1)  .setIndicator("TAB1",  this.getResources().getDrawable(R.drawable.img1)));  tabHost.addTab(tabHost.newTabSpec("Tab_2").setContent(R.id.tab2)  .setIndicator("TAB2",  this.getResources().getDrawable(R.drawable.img2)));  tabHost.addTab(tabHost.newTabSpec("Tab_3").setContent(R.id.tab3)  .setIndicator("TAB3",  this.getResources().getDrawable(R.drawable.img3)));  tabHost.setCurrentTab(1);  
//      tabHost.setBackgroundColor(Color.GRAY);  tabHost.setOnTabChangedListener(new TabHost.OnTabChangeListener() {  public void onTabChanged(String tabId) {  Dialog dialog = new AlertDialog.Builder(Test_TabWidget.this)  .setTitle("提示").setMessage(  "选中了" + tabId + "选项卡").setIcon(R.drawable.icon).setPositiveButton("确定", new DialogInterface.OnClickListener(){  public void onClick(DialogInterface dialog,  int which) {  // TODO Auto-generated method stub  }  }).create();  dialog.show();  }  });  }  
}  

第二种:就是定义我们自己的tabHost:不用继承TabActivity。

在自定义TabHost时需要注意,创建TabHost时需要以下3个步骤:
(1)在xml资源文件中创建TabHost节点,并将id设置为tabhost。
(2)创建TabWidget子节点,并设置id为tabs。
(3)创建FrameLayout子节点,用于显示内容,其id为tabcontent。
与使用TabActivity相比较不难发现,自定义TabHost时不需要继承TabActivity了,只需要简单继承Activity就可以了,这无疑给我们编程提供了更大的自由发挥的空间。因为我们知道继承虽然会给我们的编程带来很大程度的方便,但也同样带来了很多的条条   框框的限制。
让我们着重来看第二步获得TabHost对象,这里的获得TabHost对象的方法与使用TabActivity时又不一样了,具体方法为:
m_TabHost = (TabHost)findViewById(android.R.id.tabhost);
我们发现得到TabHost的方法是最简单、最常见的findViewById()方法!这里的参数就是android.R.id.tabhost,也就是在xml资源文件中我们实现定义的:
android:id="@android:id/tabhost"
需要注意的是,在获得了TabHost之后,我们需要调用:
TabHost.setup()
调用了该方法之后,TabHost才设置完成可以正常使用。而在使用TabActvity时则不需这一步,因为在getTabHost()方法中已经完成了设置的工作。
 
<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"  android:id="@+id/TabHost01" android:layout_width="fill_parent"  android:layout_height="fill_parent">  <LinearLayout android:layout_width="fill_parent"  android:orientation="vertical" android:layout_height="fill_parent">    <FrameLayout android:id="@android:id/tabcontent"                                      //这里的id一定要是"@android:id/tabcontent"android:layout_width="fill_parent"  android:layout_height="fill_parent">  <LinearLayout android:id="@+id/LinearLayout1"  android:layout_width="fill_parent"  android:layout_height="wrap_content">  <TextView android:text="one"  android:id="@+id/TextView01" android:layout_width="wrap_content"  android:layout_height="wrap_content">  </TextView>  </LinearLayout>  <LinearLayout android:id="@+id/LinearLayout2"  android:layout_width="wrap_content"  android:layout_height="wrap_content">  <TextView android:text="two"  android:id="@+id/TextView02" android:layout_width="fill_parent"  android:layout_height="wrap_content">  </TextView>  </LinearLayout>  <LinearLayout android:id="@+id/LinearLayout3"  android:layout_width="wrap_content"  android:layout_height="wrap_content">  <TextView android:text="three"  android:id="@+id/TextView03" android:layout_width="fill_parent"  android:layout_height="wrap_content">  </TextView>  </LinearLayout>  </FrameLayout>  <TabWidget android:id="@android:id/tabs"      //注意id表述方式,一定要是"@android:id/tabs"android:layout_width="fill_parent"  android:layout_height="wrap_content" /></LinearLayout> </TabHost>


import android.app.Activity;  
import android.os.Bundle;  
import android.util.Log;  
import android.view.LayoutInflater;  
import android.widget.TabHost;  public class Test_TabHost extends Activity {  /** Called when the activity is first created. */  private TabHost tabHost;  @Override  public void onCreate(Bundle savedInstanceState) {  super.onCreate(savedInstanceState);  setContentView(R.layout.main);  try{  tabHost = (TabHost) this.findViewById(R.id.TabHost01);  tabHost.setup();  tabHost.addTab(tabHost.newTabSpec("tab_1")  .setContent(R.id.LinearLayout1)  .setIndicator("TAB1",this.getResources().getDrawable(R.drawable.img1)));  tabHost.addTab(tabHost.newTabSpec("tab_2")  .setContent(R.id.LinearLayout2).setIndicator("TAB2",  this.getResources().getDrawable(R.drawable.img2)));  tabHost.addTab(tabHost.newTabSpec("tab_3")  .setContent(R.id.LinearLayout3).setIndicator("TAB3",  this.getResources().getDrawable(R.drawable.img3)));  tabHost.setCurrentTab(1);  }catch(Exception ex){  ex.printStackTrace();  Log.d("EXCEPTION", ex.getMessage());  }  }  
}  


PS
1、android中的TabActivity是专门用来放置TabHost组件的,但是很遗憾,非常固定,必须放在顶部,对于单手使用手机的人来说,很不方便。所以研究了一下如何将TabBar放置底部,具体思路是在xml布局文件中,将TabWidget放于FrameLayout的下面(两者是垂直线性的关系),并分配各个组件的权重。另外,如果想去掉Tab中那个丑陋的白线,可以通过设置其Padding实现。

2、让自己的类继承TabActivity,然后通过调用getTabHost()方法得到tabhost对象,然后把自己写好的数据展示的布局文件加载到tabhost中,就可以实现了。最后是通过调用addTab()方法添加标签的相关属性(如:标签名称,标签图片,标签内容布局)。
而如果通过XML文件配置tabHost则需要注意的是,framelayout,tabwidget标签的id都必须引用系统的id(@android:id/tabcontent,@android:id/tabs),不然会报异常.在程序用使用findViewById()加载tabhost,然后调用tabhost.setup()方法初始化tabhost,后面的步骤则和上面一种一样

这篇关于TabHost的两种使用方法的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1013439

相关文章

C# 比较两个list 之间元素差异的常用方法

《C#比较两个list之间元素差异的常用方法》:本文主要介绍C#比较两个list之间元素差异,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录1. 使用Except方法2. 使用Except的逆操作3. 使用LINQ的Join,GroupJoin

Linux中压缩、网络传输与系统监控工具的使用完整指南

《Linux中压缩、网络传输与系统监控工具的使用完整指南》在Linux系统管理中,压缩与传输工具是数据备份和远程协作的桥梁,而系统监控工具则是保障服务器稳定运行的眼睛,下面小编就来和大家详细介绍一下它... 目录引言一、压缩与解压:数据存储与传输的优化核心1. zip/unzip:通用压缩格式的便捷操作2.

MySQL查询JSON数组字段包含特定字符串的方法

《MySQL查询JSON数组字段包含特定字符串的方法》在MySQL数据库中,当某个字段存储的是JSON数组,需要查询数组中包含特定字符串的记录时传统的LIKE语句无法直接使用,下面小编就为大家介绍两种... 目录问题背景解决方案对比1. 精确匹配方案(推荐)2. 模糊匹配方案参数化查询示例使用场景建议性能优

关于集合与数组转换实现方法

《关于集合与数组转换实现方法》:本文主要介绍关于集合与数组转换实现方法,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录1、Arrays.asList()1.1、方法作用1.2、内部实现1.3、修改元素的影响1.4、注意事项2、list.toArray()2.1、方

使用Python实现可恢复式多线程下载器

《使用Python实现可恢复式多线程下载器》在数字时代,大文件下载已成为日常操作,本文将手把手教你用Python打造专业级下载器,实现断点续传,多线程加速,速度限制等功能,感兴趣的小伙伴可以了解下... 目录一、智能续传:从崩溃边缘抢救进度二、多线程加速:榨干网络带宽三、速度控制:做网络的好邻居四、终端交互

Python中注释使用方法举例详解

《Python中注释使用方法举例详解》在Python编程语言中注释是必不可少的一部分,它有助于提高代码的可读性和维护性,:本文主要介绍Python中注释使用方法的相关资料,需要的朋友可以参考下... 目录一、前言二、什么是注释?示例:三、单行注释语法:以 China编程# 开头,后面的内容为注释内容示例:示例:四

一文详解Git中分支本地和远程删除的方法

《一文详解Git中分支本地和远程删除的方法》在使用Git进行版本控制的过程中,我们会创建多个分支来进行不同功能的开发,这就容易涉及到如何正确地删除本地分支和远程分支,下面我们就来看看相关的实现方法吧... 目录技术背景实现步骤删除本地分支删除远程www.chinasem.cn分支同步删除信息到其他机器示例步骤

Go语言数据库编程GORM 的基本使用详解

《Go语言数据库编程GORM的基本使用详解》GORM是Go语言流行的ORM框架,封装database/sql,支持自动迁移、关联、事务等,提供CRUD、条件查询、钩子函数、日志等功能,简化数据库操作... 目录一、安装与初始化1. 安装 GORM 及数据库驱动2. 建立数据库连接二、定义模型结构体三、自动迁

ModelMapper基本使用和常见场景示例详解

《ModelMapper基本使用和常见场景示例详解》ModelMapper是Java对象映射库,支持自动映射、自定义规则、集合转换及高级配置(如匹配策略、转换器),可集成SpringBoot,减少样板... 目录1. 添加依赖2. 基本用法示例:简单对象映射3. 自定义映射规则4. 集合映射5. 高级配置匹

Spring 框架之Springfox使用详解

《Spring框架之Springfox使用详解》Springfox是Spring框架的API文档工具,集成Swagger规范,自动生成文档并支持多语言/版本,模块化设计便于扩展,但存在版本兼容性、性... 目录核心功能工作原理模块化设计使用示例注意事项优缺点优点缺点总结适用场景建议总结Springfox 是