Android TabWidget切換卡的實(shí)現(xiàn)應(yīng)用
TabWidget類似于Android 中查看電話薄的界面,通過多個(gè)標(biāo)簽切換顯示不同內(nèi)容。要實(shí)現(xiàn)這一效果,首先要了解TabHost,它是一個(gè)用來存放多個(gè)Tab標(biāo)簽的容器。每一個(gè)Tab都可以對(duì)應(yīng)自己的布局,比如,電話薄中的Tab布局就是一個(gè)List的線性布局了。
要使用TabHost,首先需要通過getTabHost方法來獲取TabHost的對(duì)象,然后通過addTab方法來向TabHost中添加 Tab。當(dāng)然每個(gè)Tab在切換時(shí)都會(huì)產(chǎn)生一個(gè)事件,要捕捉這個(gè)事件需要設(shè)置TabActivity的事件監(jiān)聽 setOnTabChangedListener。
1、布局文件
<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:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<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/textview1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="Linux"
android:textColor="#FF0000" />
<TextView
android:id="@+id/textview2"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="MAC"
android:textColor="#385E0F" />
<TextView
android:id="@+id/textview3"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="Windows"
android:textColor="#1E90FF" />
</FrameLayout>
</LinearLayout>
</TabHost>
2、修改MainActivity,注意是繼承自TabActivity
public class MainActivity extends TabActivity {
private TabHost tabHost;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tabHost = getTabHost();
addTab();// 添加標(biāo)簽
// 設(shè)置TabHost背景顏色
tabHost.setBackgroundColor(Color.argb(150, 20, 80, 150));
// 設(shè)置TabHost背景圖片資源
tabHost.setBackgroundResource(R.drawable.ic_launcher);
// 設(shè)置當(dāng)前顯示哪一個(gè)標(biāo)簽 我的理解就是當(dāng)你第一次啟動(dòng)程序默認(rèn)顯示那個(gè)標(biāo)簽 這里是指定的選項(xiàng)卡的ID從0開始
tabHost.setCurrentTab(0);
// 標(biāo)簽切換事件處理,setOnTabChangedListener 注意是標(biāo)簽切換事件不是點(diǎn)擊事件,而是從一個(gè)標(biāo)簽切換到另外一個(gè)標(biāo)簽會(huì)觸發(fā)的事件
tabHost.setOnTabChangedListener(new OnTabChangeListener() {
@Override
public void onTabChanged(String tabId) {
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
Dialog dia;
builder.setTitle("提示");
builder.setMessage("當(dāng)前選中了" + tabId + "標(biāo)簽");
builder.setPositiveButton("確定", new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
dia = builder.create();
dia.show();
}
});
}
// 為TabHost添加標(biāo)簽 新建一個(gè)newTabSped(new TabSpec) 設(shè)置其標(biāo)簽和圖標(biāo)(setIndicator)、設(shè)置內(nèi)容(setContent)
// TabSpec是TabHost的內(nèi)部類 TabHost對(duì)象的 newTabSpec()方法返回一個(gè)TabSpec對(duì)象
// 源碼里邊是這么寫的 public TabSpec newTabSpec(String tag)
// { return new TabSpec(tag); }
private void addTab() {
tabHost.addTab(tabHost
.newTabSpec("tab1")
.setIndicator("TAB1",
getResources().getDrawable(R.drawable.ic_launcher))// setIndicator()此方法用來設(shè)置標(biāo)簽和圖表
.setContent(R.id.textview1));
// 指定內(nèi)容為一個(gè)TextView --->public TabHost.TabSpec setContent(int viewId) 此方法需要一個(gè) viewId 作為參數(shù)
tabHost.addTab(tabHost
.newTabSpec("tab2")
.setIndicator("TAB2",
getResources().getDrawable(R.drawable.ic_launcher))
.setContent(R.id.textview2));
tabHost.addTab(tabHost
.newTabSpec("tab3")
.setIndicator("TAB3",
getResources().getDrawable(R.drawable.ic_launcher))
.setContent(R.id.textview3));
}
}
3、運(yùn)行程序:如下!



相關(guān)文章
Android打造屬于自己的新聞平臺(tái)(客戶端+服務(wù)器)
這篇文章主要為大家詳細(xì)介紹了Android打造屬于自己的新聞平臺(tái)的相關(guān)資料,Android實(shí)現(xiàn)新聞客戶端服務(wù)器,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-06-06
Android實(shí)現(xiàn)監(jiān)聽電話呼叫狀態(tài)的方法
這篇文章主要介紹了Android實(shí)現(xiàn)監(jiān)聽電話呼叫狀態(tài)的方法,涉及Android權(quán)限控制及電話狀態(tài)監(jiān)聽的相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-10-10
Android使用OKHttp包處理HTTP相關(guān)操作的基本用法講解
這篇文章主要介紹了Android使用OKHttp包處理HTTP相關(guān)操作的基本用法講解,包括操作如何利用OKHttp操作HTTP請(qǐng)求與處理緩存等內(nèi)容,需要的朋友可以參考下2016-07-07
Android中傳值Intent與Bundle的區(qū)別小結(jié)
這篇文章主要給大家總結(jié)介紹了關(guān)于Android中傳值Intent與Bundle的區(qū)別,文中通過示例代碼以及圖文介紹的非常詳細(xì),對(duì)各位Android開發(fā)者們具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2019-03-03
Android 多線程實(shí)現(xiàn)重復(fù)啟動(dòng)與停止的服務(wù)
這篇文章主要介紹了Android 多線程實(shí)現(xiàn)重復(fù)啟動(dòng)與停止的服務(wù)的相關(guān)資料,多線程環(huán)境下為了避免死鎖,一般提倡開放調(diào)用,開放調(diào)用可以避免死鎖,它的代價(jià)是失去原子性,這里說明重復(fù)啟動(dòng)與停止的服務(wù),需要的朋友可以參考下2017-08-08
Android中Binder詳細(xì)學(xué)習(xí)心得
這篇文章主要介紹了Android中Binder詳細(xì)學(xué)習(xí)心得,并分析了Binder的詳細(xì)用法,需要的朋友參考下吧。2018-01-01
Android模塊化中數(shù)據(jù)傳遞/路由跳轉(zhuǎn)實(shí)現(xiàn)示例
這篇文章主要介紹了Android模塊化中數(shù)據(jù)傳遞/路由跳轉(zhuǎn)實(shí)現(xiàn)示例,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-07-07
Android編程實(shí)現(xiàn)將時(shí)間轉(zhuǎn)化成幾分鐘前、幾天前等形式的工具類
這篇文章主要介紹了Android編程實(shí)現(xiàn)將時(shí)間轉(zhuǎn)化成幾分鐘前、幾天前等形式的工具類,涉及Android針對(duì)日期時(shí)間的相關(guān)運(yùn)算與判斷簡單操作技巧,需要的朋友可以參考下2018-02-02
Android系統(tǒng)在shell中的df命令實(shí)現(xiàn)
今天小編就為大家分享一篇關(guān)于Android系統(tǒng)在shell中的df命令實(shí)現(xiàn),小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧2018-12-12

