Android編程實(shí)現(xiàn)自定義Tab選項(xiàng)卡功能示例
本文實(shí)例講述了Android編程實(shí)現(xiàn)自定義Tab選項(xiàng)卡功能。分享給大家供大家參考,具體如下:
import android.app.TabActivity; import android.content.Intent; import android.os.Bundle; import android.widget.*; import android.widget.TabHost.OnTabChangeListener; import android.os.Build; import android.view.View; import java.lang.reflect.Field; import android.view.LayoutInflater; public class testTabActivity extends TabActivity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); int width =45; int height =48; final TabHost tabs = getTabHost(); final TabWidget tabWidget = tabs.getTabWidget(); Field mBottomLeftStrip; Field mBottomRightStrip; LayoutInflater.from(this).inflate(R.layout.tab_views, tabs.getTabContentView(), true); tabs.addTab(tabs.newTabSpec("first tab") .setIndicator("信息",getResources().getDrawable(R.drawable.m)) .setContent(new Intent(testTabActivity.this,OneActivty.class)) ); tabs.addTab(tabs.newTabSpec("second tab") .setIndicator("收藏",getResources().getDrawable(R.drawable.n)) .setContent(R.id.content)); tabs.addTab(tabs.newTabSpec("second tab") .setIndicator("設(shè)置",getResources().getDrawable(R.drawable.s)) .setContent(R.id.content)); for (int i =0; i < tabWidget.getChildCount(); i++) { /** * 設(shè)置高度、寬度,不過寬度由于設(shè)置為fill_parent,在此對(duì)它沒效果 */ tabWidget.getChildAt(i).getLayoutParams().height = height; tabWidget.getChildAt(i).getLayoutParams().width = width; /** * 設(shè)置tab中標(biāo)題文字的顏色,不然默認(rèn)為黑色 */ final TextView tv = (TextView) tabWidget.getChildAt(i).findViewById(android.R.id.title); tv.setTextColor(this.getResources().getColorStateList(android.R.color.white)); /** * 此方法是為了去掉系統(tǒng)默認(rèn)的色白的底角 * * 在 TabWidget中mBottomLeftStrip、mBottomRightStrip * 都是私有變量,但是我們可以通過反射來獲取 * * 由于還不知道Android 2.2的接口是怎么樣的,現(xiàn)在先加個(gè)判斷好一些 */ if (Float.valueOf(Build.VERSION.RELEASE) <= 2.1) { try { mBottomLeftStrip = tabWidget.getClass().getDeclaredField ("mBottomLeftStrip"); mBottomRightStrip = tabWidget.getClass().getDeclaredField ("mBottomRightStrip"); if(!mBottomLeftStrip.isAccessible()) { mBottomLeftStrip.setAccessible(true); } if(!mBottomRightStrip.isAccessible()){ mBottomRightStrip.setAccessible(true); } mBottomLeftStrip.set(tabWidget, getResources().getDrawable (R.drawable.no)); mBottomRightStrip.set(tabWidget, getResources().getDrawable (R.drawable.no)); } catch (Exception e) { e.printStackTrace(); } } else { /** * 不做任何處理 */ } View vvv = tabWidget.getChildAt(i); if(tabs.getCurrentTab()==i){ vvv.setBackgroundDrawable(getResources().getDrawable(R.drawable.tab_button)); } else { vvv.setBackgroundDrawable(getResources().getDrawable(R.drawable.bar)); } } /** * 當(dāng)點(diǎn)擊tab選項(xiàng)卡的時(shí)候,更改當(dāng)前的背景 */ tabs.setOnTabChangedListener(new OnTabChangeListener(){ @Override public void onTabChanged(String tabId) { // TODO Auto-generated method stub for (int i =0; i < tabWidget.getChildCount(); i++) { View vvv = tabWidget.getChildAt(i); if(tabs.getCurrentTab()==i){ vvv.setBackgroundDrawable(getResources().getDrawable(R.drawable.tab_button)); } else { vvv.setBackgroundDrawable(getResources().getDrawable(R.drawable.bar)); } } }}); } }
更多關(guān)于Android相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Android開發(fā)入門與進(jìn)階教程》、《Android編程之a(chǎn)ctivity操作技巧總結(jié)》、《Android資源操作技巧匯總》、《Android文件操作技巧匯總》、《Android操作SQLite數(shù)據(jù)庫技巧總結(jié)》、《Android操作json格式數(shù)據(jù)技巧總結(jié)》、《Android數(shù)據(jù)庫操作技巧總結(jié)》、《Android編程開發(fā)之SD卡操作方法匯總》、《Android視圖View技巧總結(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)類似網(wǎng)易新聞選項(xiàng)卡動(dòng)態(tài)滑動(dòng)效果
- Android編程實(shí)現(xiàn)將tab選項(xiàng)卡放在屏幕底部的方法
- Android開發(fā)之選項(xiàng)卡功能的實(shí)現(xiàn)方法示例
- Android實(shí)現(xiàn)知乎選項(xiàng)卡動(dòng)態(tài)隱藏效果實(shí)例
相關(guān)文章
Android如何實(shí)現(xiàn)底部菜單固定到底部
這篇文章主要介紹了Android如何實(shí)現(xiàn)底部菜單固定到底部,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-09-09在RecyclerView中實(shí)現(xiàn)button的跳轉(zhuǎn)功能
本次實(shí)驗(yàn)就是在RecyclerView中添加一個(gè)button控件并實(shí)現(xiàn)監(jiān)聽,使鼠標(biāo)點(diǎn)擊時(shí)可以跳轉(zhuǎn)到另外一個(gè)設(shè)計(jì)好的界面,對(duì)RecyclerView實(shí)現(xiàn)button跳轉(zhuǎn)功能感興趣的朋友一起看看吧2021-10-10Android?ScrollView實(shí)現(xiàn)滾動(dòng)超過邊界松手回彈
這篇文章主要為大家詳細(xì)介紹了Android?ScrollView實(shí)現(xiàn)滾動(dòng)超過邊界松手回彈,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-04-04Android提高之多級(jí)樹形菜單的實(shí)現(xiàn)方法
這篇文章主要介紹了Android多級(jí)樹形菜單的實(shí)現(xiàn)方法,很實(shí)用的功能,需要的朋友可以參考下2014-08-08kotlin實(shí)現(xiàn)快遞與號(hào)碼歸屬地查詢案例詳解
時(shí)間軸時(shí)一個(gè)很炫酷的效果,一般作用在物流信息上,我們同樣也可以作為一個(gè)學(xué)習(xí)對(duì)象去學(xué)習(xí)他的使用方法,同時(shí)呢,我們可以在線查詢我們的電話號(hào)碼歸屬地,巧用鍵盤的邏輯提升我們用戶體驗(yàn)2023-02-02Android 下載網(wǎng)絡(luò)圖片并顯示到本地
本文主要介紹了Android實(shí)現(xiàn)下載網(wǎng)絡(luò)圖片并顯示到本地功能的示例代碼。具有很好的參考價(jià)值,下面跟著小編一起來看下吧2017-03-03Android Studio 合并module到統(tǒng)一文件夾的方法
這篇文章主要介紹了Android Studio 合并module到統(tǒng)一文件夾的方法,補(bǔ)充介紹了android studio關(guān)于同名資源文件的合并技巧,需要的朋友可以參考下2018-04-04Android應(yīng)用開發(fā)的版本更新檢測升級(jí)功能實(shí)現(xiàn)示例
本文對(duì)Android版本更新的知識(shí)做全面的總結(jié),主要包括開發(fā)中版本的設(shè)置,如何檢測本程序的版本,版本的更新判斷和顯示,新版本程序的安裝2022-04-04Android編程實(shí)現(xiàn)下載時(shí)主界面與詳細(xì)界面一致更新的方法
這篇文章主要介紹了Android編程實(shí)現(xiàn)下載時(shí)主界面與詳細(xì)界面一致更新的方法,涉及Android事件監(jiān)聽及界面動(dòng)態(tài)更新相關(guān)操作技巧,需要的朋友可以參考下2017-11-11