Android開(kāi)發(fā)之選項(xiàng)卡功能的實(shí)現(xiàn)方法示例
本文實(shí)例講述了Android選項(xiàng)卡功能的實(shí)現(xiàn)方法。分享給大家供大家參考,具體如下:
選項(xiàng)卡(TabHost)方便的在窗口上設(shè)置多個(gè)標(biāo)簽頁(yè),每個(gè)標(biāo)簽頁(yè)相當(dāng)于獲得一個(gè)與外部容器相同大小的組件擺放區(qū)域
通過(guò)這種方式,可以在一個(gè)容器中放置多組件。
創(chuàng)建4個(gè)java文件并對(duì)應(yīng)layout
創(chuàng)建主java ,代碼
package lianxi; import com.example.jichu_lianxi.R; import android.app.TabActivity; import android.content.Intent; import android.content.res.Resources; import android.os.Bundle; import android.widget.TabHost; public class TobHost_lianxi extends TabActivity{ @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); //獲取當(dāng)前Activity的標(biāo)簽,該方法的實(shí)現(xiàn)已經(jīng)執(zhí)行了setContentView(com.android.internal.R.layout.tab_content); Resources resources = getResources(); TabHost tabHost = getTabHost(); TabHost.TabSpec spec; /* * 對(duì)方法的解釋?zhuān)? * 1. newTabSpec("artist")創(chuàng)建一個(gè)標(biāo)簽項(xiàng),其中artist為它的標(biāo)簽標(biāo)識(shí)符 * 2. setIndicator("標(biāo)簽1", resources.getDrawable(R.drawable.bulb_off)) * 顯示文本以及標(biāo)簽上的圖標(biāo)(該圖標(biāo)不是一個(gè)圖片,而是一個(gè)xml文件) */ //添加第一個(gè)標(biāo)簽 Intent intent = new Intent(TobHost_lianxi.this,KeyOnclick.class); spec = tabHost.newTabSpec("keyonclick").setIndicator("標(biāo)簽1", resources.getDrawable(R.drawable.bulb_off)).setContent(intent); tabHost.addTab(spec);//將標(biāo)簽添加到標(biāo)簽項(xiàng)中 //添加第二個(gè)標(biāo)簽 Intent intent2 = new Intent(TobHost_lianxi.this,List_lianxi.class); spec = tabHost.newTabSpec("list").setIndicator("標(biāo)簽2",resources.getDrawable(R.drawable.bulb_off)).setContent(intent2); tabHost.addTab(spec); //添加第三個(gè)標(biāo)簽 Intent intent3 = new Intent(TobHost_lianxi.this,ToggleButton_lianxi.class); spec = tabHost.newTabSpec("togglebutton").setIndicator("標(biāo)簽3",resources.getDrawable(R.drawable.bulb_off)).setContent(intent3); tabHost.addTab(spec); //添加第四個(gè)標(biāo)簽 Intent intent4 = new Intent(TobHost_lianxi.this,ToggleButton_lianxi.class); spec = tabHost.newTabSpec("toggle").setIndicator("標(biāo)簽4",resources.getDrawable(R.drawable.bulb_off)).setContent(intent4); tabHost.addTab(spec); //設(shè)置第一次打開(kāi)的默認(rèn)顯示的標(biāo)簽,參數(shù)與 .newTabSpec的參數(shù)匹配 //tabHost.setCurrentTabByTag("toggle"); //設(shè)置第一次打開(kāi)的默認(rèn)顯示的標(biāo)簽,參數(shù)代表其添加到標(biāo)簽中的順序,位置從0開(kāi)始 tabHost.setCurrentTab(1); } }
其中 KeyOnclick.java、List_lianxi.java、ToggleButton_lianxi.java 代碼不貼了
不要忘了在AndroidManifest.xml文件中修改代碼
<application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name="lianxi.Mainactivity"> <intent-filter > <action android:name="android.intent.action.MAIN"/> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name="lianxi.NewActivity"></activity> <activity android:name="lianxi.AlertDialog_lianxi"></activity> <activity android:name="lianxi.Notification_lianxi"></activity> <activity android:name="lianxi.KeyOnclick"></activity> <activity android:name="lianxi.List_lianxi"></activity> <activity android:name="lianxi.ToggleButton_lianxi"></activity> <activity android:name="lianxi.TobHost_lianxi"></activity> </application>
效果圖(第一張為標(biāo)簽2。因?yàn)閠abHost.setCurrentTab(1); 設(shè)置第2個(gè)添加的標(biāo)簽項(xiàng)為默認(rèn)顯示,從0開(kāi)始算)
更多關(guān)于Android相關(guān)內(nèi)容感興趣的讀者可查看本站專(zhuān)題:《Android開(kāi)發(fā)入門(mén)與進(jìn)階教程》、《Android調(diào)試技巧與常見(jiàn)問(wèn)題解決方法匯總》、《Android基本組件用法總結(jié)》、《Android視圖View技巧總結(jié)》、《Android布局layout技巧總結(jié)》及《Android控件用法總結(jié)》
希望本文所述對(duì)大家Android程序設(shè)計(jì)有所幫助。
- Android實(shí)現(xiàn)底部導(dǎo)航欄功能(選項(xiàng)卡)
- Android多個(gè)TAB選項(xiàng)卡切換效果
- Android仿微信底部實(shí)現(xiàn)Tab選項(xiàng)卡切換效果
- Android利用Fragment實(shí)現(xiàn)Tab選項(xiàng)卡效果
- Android實(shí)現(xiàn)類(lèi)似網(wǎng)易新聞選項(xiàng)卡動(dòng)態(tài)滑動(dòng)效果
- Android編程實(shí)現(xiàn)自定義Tab選項(xiàng)卡功能示例
- Android編程實(shí)現(xiàn)將tab選項(xiàng)卡放在屏幕底部的方法
- Android實(shí)現(xiàn)知乎選項(xiàng)卡動(dòng)態(tài)隱藏效果實(shí)例
相關(guān)文章
Android字體大小自適應(yīng)不同分辨率的解決辦法
這篇文章主要介紹了Android字體大小自適應(yīng)不同分辨率的解決辦法的相關(guān)資料,需要的朋友可以參考下2017-06-06Android編程獲取APP應(yīng)用程序基本信息輔助類(lèi)【APP名稱(chēng)、包名、圖標(biāo),版本號(hào)等】
這篇文章主要介紹了Android編程獲取APP應(yīng)用程序基本信息輔助類(lèi),可實(shí)現(xiàn)針對(duì)APP名稱(chēng)、包名、圖標(biāo),版本號(hào)等信息的獲取功能,需要的朋友可以參考下2017-12-12Android手機(jī)通過(guò)rtp發(fā)送aac數(shù)據(jù)給vlc播放的實(shí)現(xiàn)步驟
這篇文章主要介紹了Android手機(jī)通過(guò)rtp發(fā)送aac數(shù)據(jù)給vlc播放的實(shí)現(xiàn)步驟,幫助大家更好的理解和學(xué)習(xí)使用Android,感興趣的朋友可以了解下2021-04-04Android APK優(yōu)化工具Zipalign詳解
本文主要介紹Android APK優(yōu)化工具Zipalign,這里整理了相關(guān)資料,并詳細(xì)介紹如何使用Zipalign工具及使用技巧,有需要的小伙伴可以參考下2016-09-09Android中使用Vectors(2)繪制優(yōu)美的路徑動(dòng)畫(huà)
這篇文章主要介紹了Android中使用Vectors(2)繪制優(yōu)美的路徑動(dòng)畫(huà)的相關(guān)資料,需要的朋友可以參考下2016-03-03