展会信息港展会大全

android tabhost
来源:互联网   发布日期:2015-10-03 11:12:46   浏览:1531次  

导读:结构继承关系瀀甀戀氀椀挀 class TabHost extends FrameLayout implements ViewTreeObserver.OnTouchModeChan...

结构

继承关系

public class TabHost extends FrameLayout implements ViewTreeObserver.OnTouchModeChangeListener

java.lang.Object

android.view.View

android.view.ViewGroup

android.widget.FrameLayout

android.widget.TabHost

提供选项卡(Tab页)的窗口视图容器。此对象包含两个子对象:一组是用户可以选择指定Tab页的标签;另一组是FrameLayout用来显示该Tab页的内容。个别元素通常控制使用这个容器对象,而不是设置在子元素本身的值。

内部类

interface TabHost.OnTabChangeListener

接口定义了当选项卡更改时被调用的回调函数

interface TabHost.TabContentFactory

当某一选项卡被选中时生成选项卡的内容

class TabHost.TabSpec

单独的选项卡,每个选项卡都有一个选项卡指示符,内容和tag标签,以便于记录.

公共方法

public void addTab (TabHost.TabSpec tabSpec)

新增一个选项卡

参数

tabSpec 指定怎样创建指示符和内容.

public void clearAllTabs ()

从tab widget中移除所有关联到当前tab host的选项卡

public boolean dispatchKeyEvent (KeyEvent event)

分发按键事件到焦点传递路线上的下一视图。焦点传递路线从视图树的顶层开始一直到当前获取焦点的视图停止。如果此视图已经获取焦点,将分发给它自身。否则,将分发到焦点传递路线的下一节点。此方法会触发任何一个按键监听器.

参数

event 分发的按键事件

返回值

如果事件已经处理则返回true,否则返回false.

public void dispatchWindowFocusChanged (boolean hasFocus)

当窗口包含的此视图获取或丢失焦点时触发此方法.ViewGroups应该重写以路由到他的子元素

参数

hasFocus 如果窗口包含的此view依获取焦点,返回true,否则返回false.

public int getCurrentTab ()

public String getCurrentTabTag ()

public View getCurrentTabView ()

public View getCurrentView ()

public FrameLayout getTabContentView ()

获取保存tab内容的FrameLayout

public TabWidget getTabWidget ()

public TabHost.TabSpec newTabSpec (String tag)

获取一个新的TabHost.TabSpec,并关联到当前tab host

参数

tag 所需的选项卡标签(tag)

public void onTouchModeChanged (boolean isInTouchMode)

当触摸模式发生改变时调用的回调函数.

参数

isInTouchMode 如果视图结构当前处于触摸模式,返回true,否则返回false.

public void setCurrentTab (int index)

public void setCurrentTabByTag (String tag)

public void setOnTabChangedListener (TabHost.OnTabChangeListener l)

注册一个回调函数,当选项卡中的任何一个tab的选中状态发生改变时调用。

参数

l 将运行的回调函数

public void setup ()

如果使用findViewById()加载TabHost,那么在新增一个选项卡tab之前,需要调用setup()。然而,当你在一个TabActivity里使用getTabHost()获取TabHost,你就不再需要调用setup()了。(译者注:实现tab窗口的两种方法:继承activity时,使用findViewById()查找TabHost,然后调用setup();继承TabActivity,通过getTabHost()查找,此时不用调用setup())例子:

public void setup (LocalActivityManager activityGroup)

如果你使用setContent(android.content.Intent),那么当activityGroup用于加载本地activity之时,必须调用此方法。如果你拓展(继承)TabActivity将自动调用setup()方法。

参数

activityGroup 用来为选项卡内容加载activities的ativityGroup

受保护方法

protected void onAttachedToWindow ()

当视图附加到窗口上时被调用。在这个点的表面进行绘制。注意此函数确保在onDraw(Cancas)之前调用,然而它可能在第一次执行onDraw之前的任何时间被调用——包括的onMeasure(int,int)的之前或之后。

protected void onDetachedFromWindow ()

当视图从窗口分离时被调用。在这个点的表面不再有画面绘制。

TabHost广泛运用于android程序中,在程序中运用TabHost,解决了手机屏幕小,显示内容少的问题,如系统自带的拨号程序,就用了TabHost

分为拨号、通话纪录、联系人、收藏几个Tab。

定义布局文件:tabhost.xml

01

<?xml version="1.0" encoding="utf-8"?>

02

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"

03

android:layout_width="match_parent"

04

android:layout_height="match_parent" >

05

06

<LinearLayout

07

android:orientation="vertical"

08

android:layout_width="match_parent"

09

android:layout_height="wrap_content"

10

android:layout_marginRight="5dp">

11

12

<LinearLayout

13

android:id="@+id/tab_edit"

14

android:layout_width="fill_parent"

15

android:layout_height="fill_parent"

16

androidrientation="vertical">

17

<EditText

18

android:id="@+id/edit"

19

android:layout_width="wrap_content"

20

android:layout_height="wrap_content"

21

android:text="请输入检索关键字..."

22

android:textSize="18sp"/>

23

<Button

24

android:id="@+id/but"

25

android:layout_width="wrap_content"

26

android:layout_height="wrap_content"

27

android:text="搜索"/>

28

</LinearLayout>

29

<LinearLayout

30

android:id="@+id/tab_clock"

31

android:layout_width="fill_parent"

32

android:layout_height="fill_parent"

33

androidrientation="vertical">

34

<AnalogClock

35

android:id="@+id/myAnalogClock"

36

android:layout_width="wrap_content"

37

android:layout_height="wrap_content"/>

38

</LinearLayout>

39

<LinearLayout

40

android:id="@+id/tab_sex"

41

android:layout_width="fill_parent"

42

android:layout_height="fill_parent"

43

androidrientation="vertical">

44

<RadioGroup

45

android:id="@+id/sex"

46

android:layout_width="fill_parent"

47

android:layout_height="wrap_content"

48

android:orientation="vertical">

49

<RadioButton

50

android:id="@+id/male"

51

android:checked="true"

52

android:text="性别:男"/>

53

<RadioButton

54

android:id="@+id/female"

55

android:text="性别:女"/>

56

</RadioGroup>

57

</LinearLayout>

58

</LinearLayout>

59

</ScrollView>

2.java代码:

01

package com.test;

02

03

import java.util.ArrayList;

04

05

06

import java.util.HashMap;

07

import java.util.List;

08

import java.util.Map;

09

10

import com.test.R;

11

import android.app.Activity;

12

import android.app.TabActivity;

13

import android.content.Intent;

14

import android.os.Bundle;

15

import android.view.LayoutInflater;

16

import android.view.View;

17

import android.view.View.OnClickListener;

18

import android.widget.CompoundButton.OnCheckedChangeListener;

19

import android.widget.TabHost.TabSpec;

20

import android.widget.CompoundButton;

21

import android.widget.ListView;

22

import android.widget.SimpleAdapter;

23

import android.widget.TabHost;

24

import android.widget.TextView;

25

import android.widget.ToggleButton;

26

27

public class TabHostDemo extends TabActivity {

28

//自定义

29

privateTabHostmytabhost = null;// 定义TabHost

30

private int[] layRes = { R.id.tab_edit, R.id.tab_clock , R.id.tab_sex };

31

32

33

34

protected void onCreate(Bundle savedInstanceState) {

35

// TODO Auto-generated method stub

36

super.onCreate(savedInstanceState);

37

mytabhost = super.getTabHost() ;// 取得TabHost对象

38

LayoutInflater.from(this)// 取得LayoutInflater对象

39

.inflate(R.layout.tabhost,// 定义转换的布局管理器

40

this.mytabhost.getTabContentView(), // 指定标签增加的容器

41

true);// 实例化布局管理器中的组件

42

for (int x = 0; x < this.layRes.length; x++) {// 循环取出所有布局标记

43

TabSpec myTab = mytabhost.newTabSpec("tab" + x);// 定义TabSpec

44

myTab.setIndicator("标签- " + x) ;// 设置标签文字

45

myTab.setContent(this.layRes[x]) ;// 设置显示的组件

46

this.mytabhost.addTab(myTab) ;// 增加标签

47

}

48

49

}

50

}

3.执行结果:

作者:amigos_pop

赞助本站

人工智能实验室

相关热词: android开发 教程

AiLab云推荐
推荐内容
展开

热门栏目HotCates

Copyright © 2010-2024 AiLab Team. 人工智能实验室 版权所有    关于我们 | 联系我们 | 广告服务 | 公司动态 | 免责声明 | 隐私条款 | 工作机会 | 展会港