欢迎来到代码驿站!

Android代码

当前位置:首页 > 移动开发 > Android代码

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

时间:2021-03-17 09:41:07|栏目:Android代码|点击:

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利用OpenGLES绘制天空盒实例教程

栏    目:Android代码

下一篇:Android图片色彩变换实现方法

本文标题:Android组件TabHost实现页面中多个选项卡切换效果

本文地址:http://www.codeinn.net/misctech/82526.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有