tab使用 TabActivity TabHost Tabspec常用方法
生活随笔
收集整理的這篇文章主要介紹了
tab使用 TabActivity TabHost Tabspec常用方法
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
本文是參考Android官方提供的sample里面的ApiDemos的學(xué)習(xí)總結(jié)。 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在后面就詳細(xì)介紹。R.layout.tabs1的內(nèi)容: <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的內(nèi)容顯示tab1且背景為Blue,是setContent(R.id.view1)這里引用了TextView1?,F(xiàn)在就基本明白如何添加tab以及如何設(shè)置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重寫(重新實(shí)現(xiàn))父類TabHost.TabContentFactory中的方法View createTabContent(String tag)來實(shí)現(xiàn)不同tab的不同content。同時(shí)在setContent的參數(shù)設(shè)置為相應(yīng)的TabContentFactory。 原來createTabContent是在每個(gè)tab第一次顯示時(shí)才調(diào)用的,隨后再次顯示該tab就不會(huì)再次調(diào)用的,我自己用Logcat查看到的!這一點(diǎn)很關(guān)鍵,就是說在createTabContent是在tab沒有完全創(chuàng)建前調(diào)用的,這意味在createTabContent里面是不能調(diào)用getCurrentTabView等之類的函數(shù)的,否則就出錯(cuò)! 至于Views/Tabs/Content By Intent例子,就只是貼出代碼,不給截圖了: public?class?Tabs3?extends?TabActivity?{? ? ????@Override? ????protected?void?onCreate(Bundle?savedInstanceState)?{? ????????super.onCreate(savedInstanceState);? ? ????????final?TabHost?tabHost?=?getTabHost();? ? ????????tabHost.addTab(tabHost.newTabSpec("tab1")? ????????????????.setIndicator("list")? ????????????????.setContent(new?Intent(this,?List1.class)));? ? ????????tabHost.addTab(tabHost.newTabSpec("tab2")? ????????????????.setIndicator("photo?list")? ????????????????.setContent(new?Intent(this,?List8.class)));? ????????? ????????//?This?tab?sets?the?intent?flag?so?that?it?is?recreated?each?time? ????????//?the?tab?is?clicked.? ????????tabHost.addTab(tabHost.newTabSpec("tab3")? ????????????????.setIndicator("destroy")? ????????????????.setContent(new?Intent(this,?Controls2.class)? ????????????????????????.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)));? ????}? }? 效果:Tab1的內(nèi)容是List1的Activity,Tab2的是List8的Activity,Tab3的是controls2.Activity。
TabActivity
首先Android里面有個(gè)名為TabActivity來給我們方便使用。其中有以下可以關(guān)注的函數(shù): ?public TabHost getTabHost ()??獲得當(dāng)前TabActivity的TabHost public TabWidget getTabWidget ()?獲得當(dāng)前TabActivity的TabWidget public void setDefaultTab (String tag)?這兩個(gè)函數(shù)很易懂,就是設(shè)置默認(rèn)的Tab ?public void setDefaultTab (int index)??通過tab名——tag或者index(從0開始) protected void onRestoreInstanceState (Bundle state)?這兩個(gè)函數(shù)的介紹可以 protected void onSaveInstanceState (Bundle outState)?參考 Activity的生命周期TabHost
那么我們要用到的Tab載體是TabHost,需要從TabActivity.getTabHost獲取。 現(xiàn)在看看TabHost類,它有3個(gè)內(nèi)嵌類:1個(gè)類TabHost.TabSpec,2個(gè)接口TabHost.TabContentFactory和TabHost.OnTabChangeListener。后面會(huì)介紹這些類和接口。 TabHost類的一些函數(shù): public void addTab (TabHost.TabSpec tabSpec)?添加tab,參數(shù)TabHost.TabSpec通過下面的函數(shù)返回得到 public TabHost.TabSpec newTabSpec (String tag)?創(chuàng)建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)?? ? ? 設(shè)置當(dāng)前的Tab by index public void setCurrentTabByTag (String tag)?設(shè)置當(dāng)前的Tab by tag public void setOnTabChangedListener (TabHost.OnTabChangeListener l)?設(shè)置TabChanged事件的響應(yīng)處理 public void setup ()?這個(gè)函數(shù)后面介紹TabHost.TabSpec
從上面的函數(shù)可以知道如何添加tab了,要注意,這里的Tag(標(biāo)簽),不是Tab按鈕上的文字。 而要設(shè)置tab的label和content,需要設(shè)置TabHost.TabSpec類。?引用SDK里面的話——“A tab has a tab indicator, content, and a tag that is used to keep track of it.”,TabHost.TabSpec就是管理這3個(gè)東西: public String getTag () public TabHost.TabSpec setContent public TabHost.TabSpec setIndicator 我理解這里的Indicator就是Tab上的label,它可以 設(shè)置label:?setIndicator (CharSequence label) 或者同時(shí)設(shè)置label和icon:setIndicator (CharSequence label, Drawable icon) 或者直接指定某個(gè)view:?setIndicator (View view) 對于Content,就是Tab里面的內(nèi)容,可以 設(shè)置View的id:?setContent(int viewId) 或者TabHost.TabContentFactory的createTabContent(String tag)來處理:setContent(TabHost.TabContentFactory contentFactory) 或者用new Intent來引入其他Activity的內(nèi)容:setContent(Intent intent) 現(xiàn)在來看官方的Views/Tabs/Content By Id例子: 代碼TabHost.OnTabChangeListener
TabHost.OnTabChangeListener接口只有一個(gè)抽象方法onTabChanged(String tagString),明顯地,在onTabChanged(String tagString)方法里面swtich..case..來判斷tagString分別處理就行了。TabHost.setup()
在此貼出SDK doc里面的相關(guān)解釋: public void setup ()?? ? ? ? Since: API Level 1 Call setup() before adding tabs if loading TabHost using findViewById().?However,You do not need to call setup() after getTabHost() in TabActivity. Example: mTabHost = (TabHost)findViewById(R.id.tabhost); mTabHost.setup(); mTabHost.addTab(TAB_TAG_1, "Hello, world!", "Tab 1"); //我的理解是,如果要用到findViewById來獲取TabHost,然后add tabs的話,需要在addTab前call setup(); public void setup (LocalActivityManager activityGroup)?? ? ? ? Since: API Level 1 If you are using?setContent(android.content.Intent), this must be called since the activityGroup is needed to launch the local activity.?This is done for you if you extend TabActivity. Parameters activityGroup Used to launch activities for tab content.本文出自 “學(xué)習(xí)Android” 博客,請務(wù)必保留此出處http://android.blog.51cto.com/268543/315208
轉(zhuǎn)載于:https://www.cnblogs.com/littleby/p/5178736.html
總結(jié)
以上是生活随笔為你收集整理的tab使用 TabActivity TabHost Tabspec常用方法的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 《解释的工具:生活中的经济学原理》读书笔
- 下一篇: 我的2015羊年总结