Android开发之TabActivity用法实例详解

作者:炫_愛羊 时间:2022-08-09 00:09:03 

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

一.简介

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

Android开发之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,TabActivity
0
投稿

猜你喜欢

  • 举例说明自定义C++异常处理的实例

    2022-10-25 13:45:27
  • java实现人工智能化屏幕监控窗口

    2022-09-16 18:09:19
  • SpringAOP切点函数实现原理详解

    2021-09-05 11:00:59
  • Android使用OkHttp发送post请求

    2022-12-04 13:24:57
  • RadioButton实现选择后可取消选择

    2023-10-15 23:50:39
  • java基础--JDK SPI概述

    2021-09-19 11:50:16
  • Java设计模式编程之工厂方法模式的使用

    2021-10-01 03:09:15
  • 关于Java中的try-with-resources语句

    2022-10-21 16:49:54
  • Android编程判断SD卡是否存在及使用容量查询实现方法

    2022-02-08 23:15:28
  • Android开发apk反编译和二次打包教程

    2022-09-19 12:01:16
  • Java构造方法和方法重载详解

    2021-06-06 13:23:00
  • C# 使用HttpClient模拟请求的案例

    2023-10-16 18:20:14
  • Spring中的REST分页的实现代码

    2023-03-16 01:06:46
  • Java基础之从HelloWorld到面向对象

    2022-11-23 11:55:06
  • 值得收藏的2017年Java开发岗位面试题

    2023-11-29 15:22:01
  • Java C++题解leetcode886可能的二分法并查集染色法

    2023-08-29 01:12:52
  • C#实现学生成绩管理系统

    2021-06-05 22:51:31
  • Kotlin基础通关之字符串与数字类型

    2023-06-22 13:27:35
  • spring boot 日志配置详解

    2023-09-24 00:23:11
  • android实现程序自动升级到安装示例分享(下载android程序安装包)

    2023-08-12 09:37:19
  • asp之家 软件编程 m.aspxhome.com