- android
- android.accessibilityservice
- android.accounts
- android.content
- android.database.sqlite
- android.graphics
- android.location
- android.media
- android.net
- android.os
- android.text
- android.view
- android.view.inputmethod
- android.widget
- AbsListView
- AbsoluteLayout
- AbsSeekBar
- AbsSpinner
- AdapterView
- AnalogClock
- BaseAdapter
- BaseExpandableListAdapter
- Button
- CheckBox
- CheckedTextView
- Checkable
- Chronometer
- CompoundButton
- CursorAdapter
- CursorTreeAdapter
- DatePicker
- DialerFilter
- DigitalClock
- EditText
- Filter
- Filter.FilterListener
- Filter.FilterResults
- ExpandableListAdapter
- Filterable
- Gallery
- Gallery.LayoutParams
- GridView
- GridLayout
- RadioGroup
- ImageView
- HorizontalScrollView
- ImageButton
- ImageSwitcher
- FilterQueryProvider
- ListAdapter
- ListView
- MediaController
- QuickContactBadge
- RadioButton
- RatingBar
- RelativeLayout
- RemoteViews
- ResourceCursorAdapter
- ResourceCursorTreeAdapter
- Scroller
- ScrollView
- SearchView
- SeekBar
- SeekBar.OnSeekBarChangeListener
- SimpleAdapter
- SimpleCursorAdapter
- SimpleCursorTreeAdapter
- SimpleExpandableListAdapter
- SlidingDrawer
- Spinner
- SpinnerAdapter
- WrapperListAdapter
- TabHost
- TabHost.TabSpec
- TextView
- TimePicker
- Toast
- TableLayout
- TableRow
- TableRow.LayoutParams
- TabWidget
- TextSwitcher
- ToggleButton
- TwoLineListItem
- VideoView
- ViewAnimator
- ViewFlipper
- ViewSwitcher
- ZoomButtonsController
- ZoomButton
- ZoomControls
- dalvik.system
TabHost
TabHost
翻译时间:2010-11-25
版本:Android 2.3 r1
结构
继承关系
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 页的内容。个别元素通常控制使用这个容器对象,而不是设置在子元素本身的值。
(译者注:即使使用的是单个元素,也最好把它放到容器对象 ViewGroup 里)
内部类
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)
分发按键事件到焦点传递路线上的下一视图。焦点传递路线从视图树的顶层开始一直到当前获取焦点的视图停止。如果此视图已经获取焦点,将分发给它自身。否则,将分发到焦点传递路线的下一节点。此方法会触发任何一个按键监听器。
(译者注:关于 focus path,可以参考以下地址:
http://blog.csdn.net/maxleng/archive/2010/05/04/5557758.aspx )
参数
event 分发的按键事件
返回值
如果事件已经处理则返回 true,否则返回 false.
public void dispatchWindowFocusChanged (boolean hasFocus)
当窗口包含的此视图获取或丢失焦点时触发此方法.ViewGroups 应该重写以路由到他的子元素
参数
hasFocus 如果窗口包含的此 view 依获取焦点,返回 true,否则返回 false.
public int getCurrentTab ()
(译者注:获取当前选项卡的 id)
public String getCurrentTabTag ()
(译者注:当前选项卡的 Tag 标签内容)
public View getCurrentTabView ()
(译者注:获取当前选项卡的视图 view)
public View getCurrentView ()
(译者注:获取当前的视图 view)
public FrameLayout getTabContentView ()
获取保存 tab 内容的 FrameLayout
public TabWidget getTabWidget ()
(译者注:根据系统规定的 id:tabs 来找到 TabWidget,并返回,注意,这里的 ID 必须是 tabs。源代码中表示如下:
private TabWidget mTabWidget;
mTabWidget=(TabWidget)findViewById(com.android.internal.R.id.tabs); )
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)
(译者注:设置当前的选项卡
参数
Index 为当前选项卡的索引。)
public void setCurrentTabByTag (String tag)
(译者注:根据选项卡的 Tab 标签来设置当前的选项卡
参数
tag 想要被设置为当前选项卡的 tag 标签值。)
public void setOnTabChangedListener (TabHost.OnTabChangeListener l)
注册一个回调函数,当选项卡中的任何一个 tab 的选中状态发生改变时调用。
(译者注:setCurrentTab(index) 时会触发调用)
参数
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 ()
当视图从窗口分离时被调用。在这个点的表面不再有画面绘制。
补充
文章链接
史上最全的 Android 的 Tab 与 TabHost 讲解
Android UI 设计 Tab TabHost 标签页的使用
动态 Tab 页
示例代码
Java:
public class Tabs1 extends TabActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TabHost tabHost = getTabHost();
LayoutInflater.from(this).inflate(R.layout.tabs1, tabHost.getTabContentView(), true);
tabHost.addTab(tabHost.newTabSpec("tab1")
.setIndicator("tab1")
.setContent(R.id.view1));
tabHost.addTab(tabHost.newTabSpec("tab3")
.setIndicator("tab2")
.setContent(R.id.view2));
tabHost.addTab(tabHost.newTabSpec("tab3")
.setIndicator("tab3")
.setContent(R.id.view3));
}
}
XML:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView android:id="@+id/view1"
android:background="@drawable/blue"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="@string/tabs_1_tab_1"/>
<TextView android:id="@+id/view2"
android:background="@drawable/red"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="@string/tabs_1_tab_2"/>
<TextView android:id="@+id/view3"
android:background="@drawable/green"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="@string/tabs_1_tab_3"/>
</FrameLayout>
详细参见 apidemo/view/tabs1
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论