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

相关文章

Java function函数式接口的使用方法与实例

《Javafunction函数式接口的使用方法与实例》:本文主要介绍Javafunction函数式接口的使用方法与实例,函数式接口如一支未完成的诗篇,用Lambda表达式作韵脚,将代码的机械美感... 目录引言-当代码遇见诗性一、函数式接口的生物学解构1.1 函数式接口的基因密码1.2 六大核心接口的形态学

Python实现文件下载、Cookie以及重定向的方法代码

《Python实现文件下载、Cookie以及重定向的方法代码》本文主要介绍了如何使用Python的requests模块进行网络请求操作,涵盖了从文件下载、Cookie处理到重定向与历史请求等多个方面,... 目录前言一、下载网络文件(一)基本步骤(二)分段下载大文件(三)常见问题二、requests模块处理

Linux内存泄露的原因排查和解决方案(内存管理方法)

《Linux内存泄露的原因排查和解决方案(内存管理方法)》文章主要介绍了运维团队在Linux处理LB服务内存暴涨、内存报警问题的过程,从发现问题、排查原因到制定解决方案,并从中学习了Linux内存管理... 目录一、问题二、排查过程三、解决方案四、内存管理方法1)linux内存寻址2)Linux分页机制3)

vue基于ElementUI动态设置表格高度的3种方法

《vue基于ElementUI动态设置表格高度的3种方法》ElementUI+vue动态设置表格高度的几种方法,抛砖引玉,还有其它方法动态设置表格高度,大家可以开动脑筋... 方法一、css + js的形式这个方法需要在表格外层设置一个div,原理是将表格的高度设置成外层div的高度,所以外层的div需要

使用DeepSeek API 结合VSCode提升开发效率

《使用DeepSeekAPI结合VSCode提升开发效率》:本文主要介绍DeepSeekAPI与VisualStudioCode(VSCode)结合使用,以提升软件开发效率,具有一定的参考价值... 目录引言准备工作安装必要的 VSCode 扩展配置 DeepSeek API1. 创建 API 请求文件2.

使用TomCat,service输出台出现乱码的解决

《使用TomCat,service输出台出现乱码的解决》本文介绍了解决Tomcat服务输出台中文乱码问题的两种方法,第一种方法是修改`logging.properties`文件中的`prefix`和`... 目录使用TomCat,service输出台出现乱码问题1解决方案问题2解决方案总结使用TomCat,

解决IDEA使用springBoot创建项目,lombok标注实体类后编译无报错,但是运行时报错问题

《解决IDEA使用springBoot创建项目,lombok标注实体类后编译无报错,但是运行时报错问题》文章详细描述了在使用lombok的@Data注解标注实体类时遇到编译无误但运行时报错的问题,分析... 目录问题分析问题解决方案步骤一步骤二步骤三总结问题使用lombok注解@Data标注实体类,编译时

Java中使用Java Mail实现邮件服务功能示例

《Java中使用JavaMail实现邮件服务功能示例》:本文主要介绍Java中使用JavaMail实现邮件服务功能的相关资料,文章还提供了一个发送邮件的示例代码,包括创建参数类、邮件类和执行结... 目录前言一、历史背景二编程、pom依赖三、API说明(一)Session (会话)(二)Message编程客

C++中使用vector存储并遍历数据的基本步骤

《C++中使用vector存储并遍历数据的基本步骤》C++标准模板库(STL)提供了多种容器类型,包括顺序容器、关联容器、无序关联容器和容器适配器,每种容器都有其特定的用途和特性,:本文主要介绍C... 目录(1)容器及简要描述‌php顺序容器‌‌关联容器‌‌无序关联容器‌(基于哈希表):‌容器适配器‌:(

Python判断for循环最后一次的6种方法

《Python判断for循环最后一次的6种方法》在Python中,通常我们不会直接判断for循环是否正在执行最后一次迭代,因为Python的for循环是基于可迭代对象的,它不知道也不关心迭代的内部状态... 目录1.使用enuhttp://www.chinasem.cnmerate()和len()来判断for