首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 移动开发 > Android >

Android UI学习 - Tab的学习跟使用

2012-06-29 
Android UI学习 - Tab的学习和使用本文是参考Android官方提供的sample里面的ApiDemos的学习总结。TabActivi

Android UI学习 - Tab的学习和使用
本文是参考Android官方提供的sample里面的ApiDemos的学习总结。

TabActivity

首先Android里面有个名为TabActivity来给我们方便使用。其中有以下可以关注的函数:

public TabHost getTabHost () 获得当前TabActivity的TabHost

public TabWidget getTabWidget () 获得当前TabActivity 的TabWidget

public void setDefaultTab (String tag) 这两个函数很易懂,就是设置默认的Tab

public void setDefaultTab (int index) 通过tab名——tag或者index(从0开始)

protected void onRestoreInstanceState (Bundle state) 这两个函数的介绍可以

protected void onSaveInstanceState (Bundle outState) 参考 Activity的生命周期

TabHost

那么我们要用到的Tab载体是TabHost,需要从TabActivity.getTabHost获取。

现在看看TabHost类,它有3个内嵌类:1个类TabHost.TabSpec,2个接口 TabHost.TabContentFactory和TabHost.OnTabChangeListener。后面会介绍这些类和接口。

TabHost类的一些函数:

public void addTab (TabHost.TabSpec tabSpec) 添加 tab,参数TabHost.TabSpec通过下面的函数返回得到

public TabHost.TabSpec newTabSpec (String tag) 创建TabHost.TabSpec

public void clearAllTabs () remove所有的Tabs

public int getCurrentTab ()

public String getCurrentTabTag ()

public View getCurrentTabView ()

public View getCurrentView ()

public FrameLayout getTabContentView () 返回Tab content的FrameLayout

public TabWidget getTabWidget ()

public void setCurrentTab (int index) 设置当前的Tab by index

public void setCurrentTabByTag (String tag) 设置当前的Tab by tag

public void setOnTabChangedListener (TabHost.OnTabChangeListener l) 设置TabChanged事件的响应处理

public void setup () 这个函数后面介绍

TabHost.TabSpec

从上面的函数可以知道如何添加tab了,要注意,这里的Tag(标签),不是Tab按钮上的文字。

而要设置tab的label和content,需要设置TabHost.TabSpec类。 引用SDK里面的话——“A tab has a tab

indicator, content, and a tag that is used to keep track of it.”,TabHost.TabSpec就是管理这3个东西:

public String getTag ()

public TabHost.TabSpec setContent

public TabHost.TabSpec setIndicator

我理解这里的Indicator就是Tab上的label,它可以

设置label: setIndicator (CharSequence label)

或者同时设置label和icon:setIndicator (CharSequence label, Drawable icon)

或者直接指定某个view: setIndicator (View view)

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

设置View的id: setContent(int viewId)

或者TabHost.TabContentFactory的createTabContent(String tag)来处理:setContent(TabHost.TabContentFactory contentFactory)

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

现在来看官方的Views/Tabs/Content By Id例子:


代码

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));
}
}

原来在获取TabHost后,需要用LayoutInflater来得到Layout,LayoutInflater在后面就详细介绍。 R.layout.tabs1的内容:

< FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
< TextView
android:id="@+id/view1"
android:background="@drawable/blue"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="@string/tabs_1_tab_1"/ >
< TextView
android:id="@+id/view2"
android:background="@drawable/red"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="@string/tabs_1_tab_2"/ >
< TextView
android:id="@+id/view3"
android:background="@drawable/green"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="@string/tabs_1_tab_3"/ >
< /FrameLayout >
< ! -- strings.xml
< string name="tabs_1_tab_1" >tab1< /string >
< string name="tabs_1_tab_2" >tab2< /string >
< string name="tabs_1_tab_3" >tab3< /string >
-- >

原来是用FrameLayout的!

而让Tab1的内容显示tab1且背景为Blue,是setContent(R.id.view1)这里引用了TextView1。现在就基本明白如 何添加tab以及如何设置label和content了。

接下来看看Views/Tabs/Content By Factory的例子:


代码

public class Tabs2 extends TabActivity implements TabHost.TabContentFactory {
@Override protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
final TabHost tabHost = getTabHost();
tabHost.addTab(tabHost.newTabSpec("tab1") .setIndicator("tab1", getResources().getDrawable(R.drawable.star_big_on)) .setContent(this));                                  
tabHost.addTab(tabHost.newTabSpec("tab2") .setIndicator("tab2") .setContent(this));
tabHost.addTab(tabHost.newTabSpec("tab3") .setIndicator("tab3") .setContent(this));
}
public View createTabContent(String tag) {
final TextView tv = new TextView(this);
tv.setText("Content for tab with tag " + tag);
return tv;
}
}

可以看到通过override重写(重新实现)父类TabHost.TabContentFactory中的方法View createTabContent(String tag)来实现不同tab的不同content。同时在setContent的参数设置为相应的TabContentFactory。

原来createTabContent是在每个tab第一次显示时才调用的,随后再次显示该tab就不会再次调用的,我自己用Logcat查看到 的!这一点很关键,就是说在createTabContent是在tab没有完全创建前调用的,这意味在

createTabContent里面是不能调用getCurrentTabView等之类的函数的,否则就出错!

热点排行