Android組件TabHost實(shí)現(xiàn)頁面中多個(gè)選項(xiàng)卡切換效果
TabHost組件可以在界面中存放多個(gè)選項(xiàng)卡, 很多軟件都使用了改組件進(jìn)行設(shè)計(jì)。
一、基礎(chǔ)知識(shí)
TabWidget : 該組件就是TabHost標(biāo)簽頁中上部 或者 下部的按鈕, 可以點(diǎn)擊按鈕切換選項(xiàng)卡;
TabSpec : 代表了選項(xiàng)卡界面, 添加一個(gè)TabSpec即可添加到TabHost中;
-- 創(chuàng)建選項(xiàng)卡 : newTabSpec(String tag), 創(chuàng)建一個(gè)選項(xiàng)卡;
-- 添加選項(xiàng)卡 : addTab(tabSpec);
二、實(shí)例講解
TabHost的基本使用,主要是layout的聲明要使用特定的id號(hào),然后activity繼承TabActivity即可。
main.xml:
<TabHost xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@android:id/tabhost" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".Main" > <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <TabWidget android:id="@android:id/tabs" android:layout_width="match_parent" android:layout_height="wrap_content" > </TabWidget> <FrameLayout android:id="@android:id/tabcontent" android:layout_width="match_parent" android:layout_height="wrap_content" > <LinearLayout android:id="@+id/tab1" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" > <TextView android:layout_width="match_parent" android:layout_height="match_parent" android:text="aa" /> </LinearLayout> <LinearLayout android:id="@+id/tab2" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" > <TextView android:layout_width="match_parent" android:layout_height="match_parent" android:text="bb" /> </LinearLayout> </FrameLayout> </LinearLayout> </TabHost>
Main.java:
package com.app.main; import android.app.TabActivity; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.TabHost; import android.widget.TabHost.OnTabChangeListener; import android.widget.TabHost.TabSpec; import android.widget.TabWidget; public class Main extends TabActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); final TabHost tabHost = this.getTabHost(); TabSpec tab1 = tabHost.newTabSpec("tab1").setIndicator("tab1") .setContent(R.id.tab1); tabHost.addTab(tab1); TabSpec tab2 = tabHost.newTabSpec("tab2").setIndicator("tab2") .setContent(R.id.tab2); tabHost.addTab(tab2); } }
實(shí)現(xiàn)效果:
其他:
當(dāng)點(diǎn)擊tabwidget的時(shí)候,若想注冊(cè)事件監(jiān)聽器,可以使用:
1.調(diào)用
tabHost.setOnTabChangedListener(new TabChangeListener(){ public void onTabChanged(String id) { } });
這個(gè)傳入的id,就是tabwidget的indicator,這里是"tab1","tab2";
2.調(diào)用
tabWidget.getChildAt(0).setOnClickListener(new OnClickListener(){ });
以上就是本文的全部內(nèi)容,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
- Android TabHost如何實(shí)現(xiàn)頂部選項(xiàng)卡
- Android開發(fā)之TabHost選項(xiàng)卡及相關(guān)疑難解決方法
- Android TabHost選項(xiàng)卡標(biāo)簽圖標(biāo)始終不出現(xiàn)的解決方法
- android TabHost(選項(xiàng)卡)的使用方法
- android 選項(xiàng)卡(TabHost)如何放置在屏幕的底部
- Android TabLayout(選項(xiàng)卡布局)簡單用法實(shí)例分析
- Android多個(gè)TAB選項(xiàng)卡切換效果
- Android實(shí)現(xiàn)底部導(dǎo)航欄功能(選項(xiàng)卡)
- Android仿微信底部實(shí)現(xiàn)Tab選項(xiàng)卡切換效果
- android選項(xiàng)卡TabHost功能用法詳解
相關(guān)文章
Android實(shí)現(xiàn)Activity水平和垂直滾動(dòng)條的方法
這篇文章主要介紹了Android實(shí)現(xiàn)Activity水平和垂直滾動(dòng)條的方法,涉及Activity的ScrollView設(shè)置相關(guān)技巧,需要的朋友可以參考下2016-07-07Android搜索框(SearchView)的功能和用法詳解
這篇文章主要為大家詳細(xì)介紹了Android搜索框SearchView的功能和用法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-05-05Android 獲取服務(wù)器與客戶端時(shí)差的實(shí)例代碼
下面小編就為大家分享一篇Android 獲取服務(wù)器與客戶端時(shí)差的實(shí)例代碼,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-01-01利用Android中的TextView實(shí)現(xiàn)逐字顯示動(dòng)畫
在安卓程序啟動(dòng)的時(shí)候,想逐字顯示一段話,每個(gè)字都有一個(gè)從透明到不透明的漸變動(dòng)畫。那如何顯示這個(gè)效果,下面一起來看看。2016-08-08獲取Android系統(tǒng)唯一識(shí)別碼的方法
這篇文章主要介紹了獲取Android系統(tǒng)唯一識(shí)別碼的方法,涉及通過編程獲取Android系統(tǒng)硬件設(shè)備標(biāo)識(shí)的相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-10-10Android NotificationManager簡單使用詳解
這篇文章主要為大家詳細(xì)介紹了Android NotificationManager的簡單使用,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-11-11