Android开发之TabActivity用法实例详解

 更新时间:2016年03月02日 09:11:10   作者:炫_愛羊  
这篇文章主要介绍了Android开发之TabActivity用法,结合实例形式较为详细的分析了Android扩展Activity实现标签页效果的具体步骤与相关技巧,需要的朋友可以参考下

本文实例讲述了Android开发之TabActivity用法。分享给大家供大家参考,具体如下:

一.简介

TabActivity继承自Activity,目的是让同一界面容纳更多的内容。TabActivity实现标签页的功能,通过导航栏对各个页面进行管理。

二.XML布局文件

注意:

1.TabActivity的布局文件要求以TabHost作为XML布局文件的根。

2.通常我们采用线性布局,所以<TabHost> 的子元素是 <LinearLayout>。

3.<TabWidget>对应Tab
<FrameLayout>则用于包含Tab需要展示的内容
需要注意的是<TabWidget> 和<FrameLayout>的Id 必须使用系统id,分别为android:id/tabs 和 android:id/tabcontent 。
因为系统会使用者两个id来初始化TabHost的两个实例变量(mTabWidget 和 mTabContent)。

4.代码示例

<?xml version="1.0" encoding="utf-8"?>
<TabHost android:id="@android:id/tabhost"
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
  <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
  <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="match_parent">
</FrameLayout>
</LinearLayout>
</TabHost>

三.TabActivity

1.TabHost:TabHost是Tab的载体,用来管理Tab。

2.TabHost的一些函数

(1)获取

TabHost tabHost=this.getTabHost();

(2) 创建TabHost.TabSpec

public TabHost.TabSpec newTabSpec (String tag)

(3)添加tab

public void addTab (TabHost.TabSpec tabSpec)

(4)remove所有的Tabs

public void clearAllTabs ()
public int getCurrentTab ()

(5)  设置当前的Tab (by index)

public void setCurrentTab (int index)

(6) 设置当前的(Tab by tag)

public void setCurrentTabByTag (String tag)

(7)设置TabChanged事件的响应处理

public void setOnTabChangedListener (TabHost.OnTabChangeListener l)

3.TabHost.TabSpec要设置tab的label和content,需要设置TabHost.TabSpec类。TabHost.TabSpec管理:

public String getTag ()
public TabHost.TabSpec setContent
public TabHost.TabSpec setIndicator

(1)Indicator这里的Indicator 就是Tab上的label,它可以

设置label :

setIndicator (CharSequence label)

设置label和icon :

setIndicator (CharSequence label, Drawable icon)

指定某个view :

setIndicator (View view)

(2)Content对于Content ,就是Tab里面的内容,可以

设置View的id :

setContent(int viewId)

用new Intent 来引入其他Activity的内容:setContent(Intent intent)

package com.zhanglong.music;
import android.app.TabActivity;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TabHost;
public class MainActivity extends TabActivity
{
  /** Called when the activity is first created. */
  @Override
  public void onCreate(Bundle savedInstanceState)
  {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
     WindowManager.LayoutParams.FLAG_FULLSCREEN);
    setContentView(R.layout.main);
    Resources res = getResources();
    TabHost tabHost = getTabHost();
    TabHost.TabSpec spec;
    Intent intent;
    intent = new Intent().setClass(this, ListActivity.class);
    spec = tabHost.newTabSpec("音乐").setIndicator("音乐",
             res.getDrawable(R.drawable.item))
           .setContent(intent);
    tabHost.addTab(spec);
    intent = new Intent().setClass(this, ArtistsActivity.class);
    spec = tabHost.newTabSpec("艺术家").setIndicator("艺术家",
             res.getDrawable(R.drawable.artist))
           .setContent(intent);
    tabHost.addTab(spec);
    intent = new Intent().setClass(this, AlbumsActivity.class);
    spec = tabHost.newTabSpec("专辑").setIndicator("专辑",
             res.getDrawable(R.drawable.album))
           .setContent(intent);
    tabHost.addTab(spec);
    intent = new Intent().setClass(this, SongsActivity.class);
    spec = tabHost.newTabSpec("最近播放").setIndicator("最近播放",
             res.getDrawable(R.drawable.album))
           .setContent(intent);
    tabHost.addTab(spec);
    tabHost.setCurrentTab(0);
  }
}

更多关于Android相关内容感兴趣的读者可查看本站专题:《Android开发入门与进阶教程》、《Android多媒体操作技巧汇总(音频,视频,录音等)》、《Android基本组件用法总结》、《Android视图View技巧总结》、《Android布局layout技巧总结》及《Android控件用法总结

希望本文所述对大家Android程序设计有所帮助。

相关文章

  • Android TextView对齐的两种方法

    Android TextView对齐的两种方法

    这篇文章主要介绍了Android TextView对齐的两种方法的相关资料,在开发Android APP 的时候经常会用到TextView 输入用户信息或者其他信息,总是不能对齐,这里提供两种方法,需要的朋友可以参考下
    2017-07-07
  • Android实现图片在屏幕内缩放和移动效果

    Android实现图片在屏幕内缩放和移动效果

    这篇文章主要为大家详细介绍了Android控制图片在屏幕内缩放和移动效果,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2019-02-02
  • Android Studio开发中Gradle各种常见报错问题解决方案

    Android Studio开发中Gradle各种常见报错问题解决方案

    这篇文章主要为大家介绍了Android Studio开发中Gradle各种常见报错问题解决方案,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2023-12-12
  • 通俗易通讲解Android蓝牙键值适配

    通俗易通讲解Android蓝牙键值适配

    这篇文章介绍了Android蓝牙键值适配的方法,对大家的学习或工作具有一定的参考借鉴价值。需要的朋友可以收藏下,方便下次浏览观看
    2021-12-12
  • flutter的环境安装配置问题及解决方法

    flutter的环境安装配置问题及解决方法

    Flutter是Google推出的基于Dart语言开发的跨平台开源UI框架,旨在统一纷纷扰扰的跨平台开发框架,在UI层面上多端共用一套Dart代码来实现多平台适配开发,这篇文章主要介绍了flutter的环境安装配置问题,需要的朋友可以参考下
    2020-06-06
  • Android自定义弹窗提醒控件使用详解

    Android自定义弹窗提醒控件使用详解

    这篇文章主要为大家详细介绍了Android自定义弹窗提醒控件的使用方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2018-07-07
  • Android中Glide加载到RelativeLayout背景图方法示例

    Android中Glide加载到RelativeLayout背景图方法示例

    Glide框架大家应该都很熟悉,我们可以使用Glide加载网络图片、加载gif图片,使用简单。下面这篇文章主要给大家介绍了关于Android中Glide加载到RelativeLayout背景图的相关资料,需要的朋友可以参考下。
    2017-12-12
  • Android Button点击事件的四种实现方法

    Android Button点击事件的四种实现方法

    这篇文章主要为大家详细介绍了Android Button点击事件的四种实现方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-07-07
  • Android自定义View实现选座功能

    Android自定义View实现选座功能

    这篇文章主要介绍了Android自定义View实现选座功能,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2020-09-09
  • android自定义对话框实例代码

    android自定义对话框实例代码

    大家好,本篇文章主要讲的是android自定义对话框实例代码,感兴趣的同学赶快来看一看吧,对你有帮助的话记得收藏一下,方便下次浏览
    2021-12-12

最新评论