Android切換卡TabWidget用法示例
本文實(shí)例講述了Android切換卡TabWidget用法。分享給大家供大家參考,具體如下:
Tab選項(xiàng)卡類似與電話本的界面,通過多個(gè)標(biāo)簽切換不同的內(nèi)容,要實(shí)現(xiàn)這個(gè)效果,首先要知道TabHost,它是一個(gè)用來存放多個(gè)Tab標(biāo)簽的容器,每一個(gè)Tab都可以對(duì)應(yīng)自己的布局,比如,電話本中的Tab布局就是一個(gè)線性布局
要使用TabHost,首先要通過getTabHost方法獲取TabHost的對(duì)象,然后通過addTab方法來向TabHost中添加Tab,當(dāng)然每個(gè)Tab在切換時(shí)都會(huì)產(chǎn)生一個(gè)事件,要捕捉這個(gè)事件,需要設(shè)置TabActivity的事件監(jiān)聽setOnTabChangedListener
下面是個(gè)小例子:
TabTest.java:
package org.hualang.tab; import android.app.Activity; import android.app.TabActivity; import android.graphics.Color; import android.os.Bundle; import android.widget.TabHost; import android.widget.Toast; import android.widget.TabHost.OnTabChangeListener; public class TabTest extends TabActivity { /** Called when the activity is first created. */ TabHost tabhost; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); //取得TabHost對(duì)象 tabhost = getTabHost(); //為TabHost添加標(biāo)簽 //新建一個(gè)newTabSpec(newTabSpec) //設(shè)置其標(biāo)簽和圖標(biāo)(setIndicator) //設(shè)置內(nèi)容(setContent) tabhost.addTab(tabhost.newTabSpec("tab1") .setIndicator("TAB 1",getResources().getDrawable(R.drawable.img1)) .setContent(R.id.text1)); tabhost.addTab(tabhost.newTabSpec("tab2") .setIndicator("TAB 2",getResources().getDrawable(R.drawable.img2)) .setContent(R.id.text2)); tabhost.addTab(tabhost.newTabSpec("tab3") .setIndicator("TAB 3",getResources().getDrawable(R.drawable.img3)) .setContent(R.id.text3)); //設(shè)置TabHost的背景顏色 //tabhost.setBackgroundColor(Color.argb(150,22,70,150)); //設(shè)置TabHost的背景圖片資源 tabhost.setBackgroundResource(R.drawable.bg0); //設(shè)置當(dāng)前顯示哪個(gè)標(biāo)簽 tabhost.setCurrentTab(0); //標(biāo)簽切換事件處理,setOnTabChangedListener tabhost.setOnTabChangedListener(new OnTabChangeListener() { public void onTabChanged(String tabId) { Toast toast=Toast.makeText(getApplicationContext(), "現(xiàn)在是"+tabId+"標(biāo)簽", Toast.LENGTH_SHORT); toast.show(); } }); } }
main.xml:
<?xml version="1.0" encoding="utf-8"?> <TabHost xmlns:android="http://schemas.android.com/apk/res/android" android:id="@android:id/tabhost" android:layout_width="fill_parent" android:layout_height="fill_parent"> <LinearLayout android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <TabWidget android:id="@android:id/tabs" android:layout_width="fill_parent" android:layout_height="wrap_content" /> <FrameLayout android:id="@android:id/tabcontent" android:layout_width="fill_parent" android:layout_height="fill_parent"> <TextView android:id="@+id/text1" android:layout_width="fill_parent" android:layout_height="fill_parent" android:text="選項(xiàng)卡1" /> <TextView android:id="@+id/text2" android:layout_width="fill_parent" android:layout_height="fill_parent" android:text="選項(xiàng)卡2" /> <TextView android:id="@+id/text3" android:layout_width="fill_parent" android:layout_height="fill_parent" android:text="選項(xiàng)卡3" /> </FrameLayout> </LinearLayout> </TabHost>
更多關(guān)于Android相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Android控件用法總結(jié)》、《Android視圖View技巧總結(jié)》、《Android操作SQLite數(shù)據(jù)庫技巧總結(jié)》、《Android操作json格式數(shù)據(jù)技巧總結(jié)》、《Android數(shù)據(jù)庫操作技巧總結(jié)》、《Android文件操作技巧匯總》、《Android編程開發(fā)之SD卡操作方法匯總》、《Android開發(fā)入門與進(jìn)階教程》及《Android資源操作技巧匯總》
希望本文所述對(duì)大家Android程序設(shè)計(jì)有所幫助。
相關(guān)文章
Android Studio真機(jī)無線連接USB設(shè)備調(diào)試運(yùn)行詳解流程
你在Android Studio寫app時(shí)是否也有想過如果可以不用數(shù)據(jù)線連接手機(jī)調(diào)試運(yùn)行就好了?如果需要取出數(shù)據(jù)線插接的話我肯定是嫌麻煩的,但是模擬器有時(shí)候需要測(cè)試一些需要硬件支持的功能時(shí)又不管用,所以最好的測(cè)試還是在真機(jī)上,本篇教你扔掉數(shù)據(jù)線來無線調(diào)試2021-11-11Android自定義ViewGroup實(shí)現(xiàn)絢麗的仿支付寶咻一咻雷達(dá)脈沖效果
這篇文章主要介紹了Android自定義ViewGroup實(shí)現(xiàn)絢麗的仿支付寶咻一咻雷達(dá)脈沖效果的相關(guān)資料,需要的朋友可以參考下2016-10-10ViewPager+RadioGroup實(shí)現(xiàn)左右滑動(dòng)卡片布局
這篇文章主要為大家詳細(xì)介紹了ViewPager+RadioGroup實(shí)現(xiàn)左右滑動(dòng)卡片布局,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-02-02Android中多個(gè)ContentProvider的初始化順序詳解
在日常Android開發(fā)中經(jīng)常會(huì)寫一些sdk來供他人或者自己調(diào)用,一般這些sdk都涉及到初始化,下面這篇文章主要給大家介紹了關(guān)于Android中多個(gè)ContentProvider的初始化順序的相關(guān)資料,需要的朋友可以參考下2022-04-04實(shí)例講解Android中ViewPager組件的一些進(jìn)階使用技巧
這篇文章主要介紹了Android中ViewPager組件的一些進(jìn)階使用技巧,包括添加標(biāo)題與onPagerChangeListener監(jiān)聽使用等,需要的朋友可以參考下2016-03-03Android AnalogClock簡(jiǎn)單使用方法實(shí)例
這篇文章主要介紹了Android AnalogClock簡(jiǎn)單使用方法,結(jié)合實(shí)例形式簡(jiǎn)單分析了AnalogClock的布局調(diào)用技巧,需要的朋友可以參考下2016-01-01Android實(shí)現(xiàn)高德地圖首頁效果(下)
這篇文章主要為大家詳細(xì)介紹了基于Android實(shí)現(xiàn)高德地圖首頁效果下篇,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2023-08-08Android實(shí)現(xiàn)靜音檢測(cè)功能
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)靜音檢測(cè)功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-01-01Android實(shí)現(xiàn)局部圖片滑動(dòng)指引效果示例
現(xiàn)在滑動(dòng)效果用的比較多,尤其是在手機(jī)端上面,本文介紹了Android實(shí)現(xiàn)局部圖片滑動(dòng)指引效果示例,現(xiàn)在就分享給大家,也給大家做個(gè)參考。2016-10-10