Android组件TabHost实现页面中多个选项卡切换效果

作者:lijiao 时间:2023-03-05 22:07:35 

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);

}

}

实现效果:

Android组件TabHost实现页面中多个选项卡切换效果

其他:

当点击tabwidget的时候,若想注册事件 * ,可以使用:

1.调用


tabHost.setOnTabChangedListener(new TabChangeListener(){

public void onTabChanged(String id)

{
   }

});

这个传入的id,就是tabwidget的indicator,这里是"tab1","tab2";

2.调用


tabWidget.getChildAt(0).setOnClickListener(new OnClickListener(){

});
标签:TabHost,选项卡,切换
0
投稿

猜你喜欢

  • Java输出链表倒数第k个节点

    2023-03-22 01:22:34
  • java实现操作系统中的最佳置换Optimal算法

    2023-10-26 10:27:13
  • Android 游戏开发入门简单示例

    2023-05-02 07:29:56
  • C#队列Queue用法实例分析

    2023-02-27 22:35:14
  • Android中volley封装实践记录(二)

    2023-05-17 02:37:13
  • Springboot笔记之热部署及不生效的解决方案

    2023-11-03 02:40:00
  • Java DWR内存泄漏问题解决方案

    2022-02-28 02:35:07
  • C# 游戏外挂实现核心代码

    2021-12-28 14:47:40
  • C++ boost::asio编程-异步TCP详解及实例代码

    2021-12-28 03:36:52
  • Flutter Animation实现缩放和滑动动画效果

    2021-09-02 10:33:12
  • 通过C++程序示例理解设计模式中的外观模式

    2022-02-06 19:38:15
  • Lombok中@EqualsAndHashCode注解的使用及说明

    2023-11-30 04:47:05
  • Android编程中关于单线程模型的理解与分析

    2022-11-11 00:34:17
  • C# 获取指定QQ头像绘制圆形头像框GDI(Graphics)的方法

    2023-03-26 14:44:41
  • C# 创建、部署和调用WebService简单示例

    2023-08-15 13:50:07
  • Spring创建bean实例的几种方式分享

    2022-02-21 22:08:19
  • Android需要提升权限的操作方法

    2021-07-17 11:25:47
  • 浅谈Java中向上造型向下造型和接口回调中的问题

    2023-11-09 13:51:46
  • Java 反射机制详解及实例代码

    2023-07-13 15:22:29
  • SpringBoot集成Redis的实现示例

    2022-10-22 14:50:33
  • asp之家 软件编程 m.aspxhome.com