TabHost是一種非常使用的組件,TabHost可以方便的在窗口上放置多個標簽頁,每個標簽頁相當(dāng)于或得了一個與外部容器相同大小的組件擺放區(qū)域。
創(chuàng)新互聯(lián)專注于蒙陰企業(yè)網(wǎng)站建設(shè),響應(yīng)式網(wǎng)站設(shè)計,成都做商城網(wǎng)站。蒙陰網(wǎng)站建設(shè)公司,為蒙陰等地區(qū)提供建站服務(wù)。全流程按需定制設(shè)計,專業(yè)設(shè)計,全程項目跟蹤,創(chuàng)新互聯(lián)專業(yè)和態(tài)度為您提供的服務(wù)與TabHost 結(jié)合使用的組件
TabWidget:代表選項卡的標簽條。
TabSpec:代表選項卡的一個Tab頁面。
創(chuàng)建添加選項卡的方法:
newTabSpec():創(chuàng)建選項卡
addTab():添加選項卡
使用TabHost的步驟:
在界面布局中定義TabHost組件
Activity繼承TabActivity
調(diào)用TabActivity的getTabHost()方法獲取TabHost
通過TabHost對象的方法來創(chuàng)建添加選項卡。
如果程序需要監(jiān)控TabHost里當(dāng)前標簽頁的改變,可以為他設(shè)置TabHost.onTabChangeListener 的監(jiān)聽器。
實例:1
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content"> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="毓璜頂小學(xué)" android:textSize="20sp" /> </LinearLayout> <TabHost android:layout_width="match_parent" android:layout_height="match_parent" android:id="@android:id/tabhost" android:layout_weight="1" ><!--引用android系統(tǒng)已有的id--> <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="match_parent"> <!--定義第一個標簽頁的內(nèi)容--> <LinearLayout android:id="@+id/tab01" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <TextView android:layout_width="fill_parent" android:layout_height="fill_parent" android:text="消息頁面" android:textSize="30sp" /> </LinearLayout> <LinearLayout android:id="@+id/tab02" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <TextView android:layout_width="fill_parent" android:layout_height="fill_parent" android:text="班級頁面" android:textSize="30sp" /> </LinearLayout> <LinearLayout android:id="@+id/tab03" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <TextView android:layout_width="fill_parent" android:layout_height="fill_parent" android:text="我的頁面" android:textSize="30sp" /> </LinearLayout> </FrameLayout> </LinearLayout> </TabHost> </LinearLayout>
注意:
android:id="@android:id/tabhost" android:id="@android:id/tabs" android:id="@android:id/tabcontent" 引用android系統(tǒng)已有的id
public class MainActivity extends TabActivity{ @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main_activity); //獲取tabhost組件 TabHost tabHost=getTabHost(); //創(chuàng)建第一個Tab頁 TabHost.TabSpec tab1=tabHost.newTabSpec("tab1") .setIndicator("班級")//設(shè)置標題 .setContent(R.id.tab01); //添加第一個tab頁 tabHost.addTab(tab1); TabHost.TabSpec tab2=tabHost.newTabSpec("tab2") .setIndicator("消息")//設(shè)置標題 .setContent(R.id.tab02); //添加第一個tab頁 tabHost.addTab(tab2); TabHost.TabSpec tab3=tabHost.newTabSpec("tab3") .setIndicator("我")//設(shè)置標題 .setContent(R.id.tab03); //添加第一個tab頁 tabHost.addTab(tab3); } }
注意:最新版本的Android推薦使用Fragment 代替TabActivity
另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國服務(wù)器、虛擬主機、免備案服務(wù)器”等云主機租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務(wù)可用性高、性價比高”等特點與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場景需求。
網(wǎng)頁標題:Android學(xué)習(xí)——TabHost的使用一-創(chuàng)新互聯(lián)
路徑分享:http://muchs.cn/article44/dhejee.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供全網(wǎng)營銷推廣、網(wǎng)站內(nèi)鏈、網(wǎng)站導(dǎo)航、手機網(wǎng)站建設(shè)、商城網(wǎng)站、品牌網(wǎng)站設(shè)計
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容