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

 更新时间:2016年05月25日 15:20:34   投稿:lijiao  
这篇文章主要为大家详细介绍了Android组件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(){


});

以上就是本文的全部内容,希望能给大家一个参考,也希望大家多多支持脚本之家。

相关文章

  • Android 未读消息的红点显示

    Android 未读消息的红点显示

    本篇文章主要介绍了"Android基础—未读消息的红点显示", 在很多APP里面,经常会看到未读消息的小红点,如下图:这个功能用到的是一个控件,叫做BadgeView。 BadgeView的用法很简单,直接把jar文件导入
    2017-04-04
  • Android仿QQ消息提示点拖拽功能

    Android仿QQ消息提示点拖拽功能

    这篇文章主要为大家详细介绍了Android仿QQ消息提示点拖拽功能,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2019-01-01
  • Android实现Activity水平和垂直滚动条的方法

    Android实现Activity水平和垂直滚动条的方法

    这篇文章主要介绍了Android实现Activity水平和垂直滚动条的方法,涉及Activity的ScrollView设置相关技巧,需要的朋友可以参考下
    2016-07-07
  • Android搜索框(SearchView)的功能和用法详解

    Android搜索框(SearchView)的功能和用法详解

    这篇文章主要为大家详细介绍了Android搜索框SearchView的功能和用法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-05-05
  • Android 获取服务器与客户端时差的实例代码

    Android 获取服务器与客户端时差的实例代码

    下面小编就为大家分享一篇Android 获取服务器与客户端时差的实例代码,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2018-01-01
  • 利用Android中的TextView实现逐字显示动画

    利用Android中的TextView实现逐字显示动画

    在安卓程序启动的时候,想逐字显示一段话,每个字都有一个从透明到不透明的渐变动画。那如何显示这个效果,下面一起来看看。
    2016-08-08
  • 获取Android系统唯一识别码的方法

    获取Android系统唯一识别码的方法

    这篇文章主要介绍了获取Android系统唯一识别码的方法,涉及通过编程获取Android系统硬件设备标识的相关技巧,具有一定参考借鉴价值,需要的朋友可以参考下
    2015-10-10
  • Android View类与SurfaceView类详解

    Android View类与SurfaceView类详解

    本文主要介绍Android View类与SurfaceView类,这里提供了详细的Android View类和SurfaceView类的使用方法,有兴趣的小伙伴可以参考下
    2016-08-08
  • android自定义左侧滑出菜单效果

    android自定义左侧滑出菜单效果

    这篇文章主要为大家详细介绍了android自定义左侧滑出菜单效果,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2022-04-04
  • Android NotificationManager简单使用详解

    Android NotificationManager简单使用详解

    这篇文章主要为大家详细介绍了Android NotificationManager的简单使用,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2021-11-11

最新评论