Android组件TabHost实现页面中多个选项卡切换效果
作者:lijiao 发布时间:2023-03-05 22:07:35
标签:TabHost,选项卡,切换
TabHost组件可以在界面中存放多个选项卡, 很多软件都使用了改组件进行设计。
一、基础知识
TabWidget : 该组件就是TabHost标签页中上部 或者 下部的按钮, 可以点击按钮切换选项卡;
TabSpec : 代表了选项卡界面, 添加一个TabSpec即可添加到TabHost中;
-- 创建选项卡 : newTabSpec(String tag), 创建一个选项卡;
-- 添加选项卡 : addTab(tabSpec);
二、实例讲解
TabHost的基本使用,主要是layout的声明要使用特定的id号,然后activity继承TabActivity即可。
main.xml:
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@android:id/tabhost"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Main" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TabWidget
android:id="@android:id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</TabWidget>
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<LinearLayout
android:id="@+id/tab1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="aa" />
</LinearLayout>
<LinearLayout
android:id="@+id/tab2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="bb" />
</LinearLayout>
</FrameLayout>
</LinearLayout>
</TabHost>
Main.java:
package com.app.main;
import android.app.TabActivity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TabHost;
import android.widget.TabHost.OnTabChangeListener;
import android.widget.TabHost.TabSpec;
import android.widget.TabWidget;
public class Main extends TabActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final TabHost tabHost = this.getTabHost();
TabSpec tab1 = tabHost.newTabSpec("tab1").setIndicator("tab1")
.setContent(R.id.tab1);
tabHost.addTab(tab1);
TabSpec tab2 = tabHost.newTabSpec("tab2").setIndicator("tab2")
.setContent(R.id.tab2);
tabHost.addTab(tab2);
}
}
实现效果:
其他:
当点击tabwidget的时候,若想注册事件 * ,可以使用:
1.调用
tabHost.setOnTabChangedListener(new TabChangeListener(){
public void onTabChanged(String id)
{
}
});
这个传入的id,就是tabwidget的indicator,这里是"tab1","tab2";
2.调用
tabWidget.getChildAt(0).setOnClickListener(new OnClickListener(){
});


猜你喜欢
- 先介绍下一些基本定义串行通信:通过的是PLC上的串行口RS232/RS422/485口,上位机链接系统 Hostlink系统是对于FA系统一
- SimpleDateFormat类:SimpleDateFormat是-一个以与语言环境有关的方式来格式化和解析日期的具体类。进行格式化(日
- 本文实例讲述了Java定义泛型方法。分享给大家供大家参考,具体如下:一 点睛1 如果定义类、接口是没有使用类型形参,但定义方法时想自己定义类
- 下面给大家介绍几种比较常见的解决办法,具体内容如下:1.有时候eclipse不自动编译,把project clean一下,让R.java重新
- 每次滑动至底端,从数据库中获取10条数据,并加载于ListView中数据库package com.example.listviewbatch
- 前言有些朋友可能是从事开发工作的时间不是特别的长,所以觉得Service相对与另外两个组件activity、broadcast receiv
- 1、JDK:Java Development Kit,java开发工具包。http://www.oracle.com/technetwork
- 避免"索引越界"错误的规则如下(针对C++):不要使用静态或动态分配的数组,改用array或vector模板不要使用带方
- 这个CardStackViewpager的灵感来自Github上面的 FlippableStackView开源项目,而我想实现的效果方向上恰
- 本文简单介绍如何引入validation的步骤,如何通过自定义validation减少代码量,提高生产力。特别提及:非基本类型属性的vali
- 由于我们在eclipse ee中把项目部署在web端经常会出现报404错误。原因为:404状态码是一种http状态码,其意思是: 所请求的页
- 在程序中对文件操作是非常常见的,而对文件的操作则不可避免的需要文件的路径,并对文件的路径进行一系列的操作,例如:判断已知的路径是一个目录还是
- Android部分手机会有虚拟按键,而没有实体按键,例如华为系列的手机。然而在开发过程中,有时候会涉及底部视图的开发,最终的结果却因为虚拟按
- 我们在SpringBoot和MyBatis整合的时候,需要在SpringBoot中通过注解方式配置事务回滚1 Pojo类package co
- 系统参数系统级全局变量,该参数在程序中任何位置都可以访问到。优先级最高,覆盖程序中同名配置。系统参数的标准格式为:-Dargname=arg
- 本文实例讲述了C#实现XSL转换的方法。分享给大家供大家参考,具体如下:xsl 可方便的将一种格式的xml,转换成另一种格式的xml,参考下
- 一、基本特点1. 开始时是乐观锁, 如果锁冲突频繁, 就转换为悲观锁.2. 开始是轻量级锁实现, 如果锁被持有的时间较长, 就转换成重量级锁
- json好久没用了,今天在用到json的时候,发现对字符串做解析的时候总是多出双引号。代码如下:string jsonText = &quo
- 向上转型:子类对象转为父类,父类可以是接口。公式:Father f = new Son();Father是父类或接口,son是子类。向下转型
- Android 实现记住用户名和密码的功能是通过SharedPreference 存储来实现的。创建一个复选按钮,通过按钮的否选取来进行事件