Android TabHost選項(xiàng)卡標(biāo)簽圖標(biāo)始終不出現(xiàn)的解決方法
本文實(shí)例分析了Android TabHost選項(xiàng)卡標(biāo)簽圖標(biāo)始終不出現(xiàn)的解決方法。分享給大家供大家參考,具體如下:
在學(xué)習(xí)Android TabHost布局過(guò)程中,很多教程告訴我,這樣來(lái)顯示選項(xiàng)卡標(biāo)簽的圖標(biāo)和文字:
TapSpec spec1 = tabHost.newTabSpec("tab 1");
spec1.setIndicator("選項(xiàng)卡一", getResources().getDrawable(R.drawable.tab_icon));
spec1.setContent(R.id.tab1);
tabHost.addTab(spec1);
折騰來(lái)折騰去,setIndicator(label, drawable)這個(gè)方法始終不能將標(biāo)題文字與圖標(biāo)一起顯示出來(lái),只有文字標(biāo)題。
在沒(méi)將電腦砸了之前,通過(guò)萬(wàn)能的stackoverflow.com終于知道確切答案以及相應(yīng)方法了:
http://stackoverflow.com/questions/10745092/icon-in-tab-is-not-showing-up
其實(shí)就是SDK 4.03(冰激凌)下:只有文字標(biāo)題顯示,圖標(biāo)是不顯示的。如果將文字標(biāo)題設(shè)置為空字符串,則此時(shí)圖標(biāo)可顯示。
對(duì)于冰激凌下兩全其美的方法,只能是自定義標(biāo)簽卡布局,創(chuàng)建一個(gè)包含ImageView和TextView組件的界面布局文件 tab_indicator.xml(layout/tab_indicator.xml),然后用setIndicator(View view)方法來(lái)設(shè)置TabSpec的界面布局。
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="0dip" android:layout_height="64dip" android:layout_weight="1" android:orientation="vertical" android:background="@drawable/tab_indicator" android:padding="5dp"> <ImageView android:id="@+id/icon" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" /> <TextView android:id="@+id/title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_centerHorizontal="true" style="?android:attr/tabWidgetStyle" /> </RelativeLayout>
接著我們可以在drawable圖片資源目錄下創(chuàng)建一個(gè)tab_info.xml文件,用來(lái)指示Tab圖標(biāo)的各狀態(tài)。
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:drawable="@drawable/tab_info_dark" android:state_selected="true" /> <item android:drawable="@drawable/tab_info_light" /> </selector>
現(xiàn)在就可以通過(guò)下面的代碼將我們自定義的視圖作為一個(gè)indicator配置給TapSpec對(duì)象。
private void addTab(String label, int drawableId) {
Intent intent = new Intent(this, MockActivity.class);
TabHost.TabSpec spec = tabHost.newTabSpec(label);
View tabIndicator = LayoutInflater.from(this).inflate(R.layout.tab_indicator, getTabWidget(), false);
TextView title = (TextView) tabIndicator.findViewById(R.id.title);
title.setText(label);
ImageView icon = (ImageView) tabIndicator.findViewById(R.id.icon);
icon.setImageResource(drawableId);
spec.setIndicator(tabIndicator);
spec.setContent(intent);
tabHost.addTab(spec);
}
像以下方式那樣調(diào)用上面那自定義addTab方法
tabHost = getTabHost(); //tabHost is a private field
addTab("First", R.drawable.tab_info);
addTab("Second", R.drawable.tab_info);
addTab("Third", R.drawable.tab_info);
注意:當(dāng)用自定義視圖的indicator來(lái)添加Tab時(shí),要將strip_enabled屬性設(shè)置為false。若要兼顧底部strip,那在添加最后一個(gè)Tab后設(shè)置getTabWidget().setStripEnabled(true);
更多關(guān)于Android相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《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 TabHost如何實(shí)現(xiàn)頂部選項(xiàng)卡
- Android開(kāi)發(fā)之TabHost選項(xiàng)卡及相關(guān)疑難解決方法
- Android組件TabHost實(shí)現(xiàn)頁(yè)面中多個(gè)選項(xiàng)卡切換效果
- android TabHost(選項(xiàng)卡)的使用方法
- android 選項(xiàng)卡(TabHost)如何放置在屏幕的底部
- Android TabLayout(選項(xiàng)卡布局)簡(jiǎn)單用法實(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 TextWatcher監(jiān)控EditText中的輸入內(nèi)容并限制其個(gè)數(shù)
本篇文章主要介紹了Android TextWatcher監(jiān)控EditText中的輸入內(nèi)容并限制其個(gè)數(shù),我們可以通過(guò)TextWatcher去觀察輸入框中輸入的內(nèi)容,有興趣的可以了解一下。2017-04-04
Android ViewPager向?qū)ы?yè)面制作方法
這篇文章主要為大家詳細(xì)介紹了Android ViewPager向?qū)ы?yè)面制作方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-11-11
Android實(shí)戰(zhàn)APP啟動(dòng)速度優(yōu)化
本篇文章給大家通過(guò)實(shí)戰(zhàn)總結(jié)了Android開(kāi)發(fā)APP啟動(dòng)速度優(yōu)化的方法以及需要注意的地方,有需要的朋友可以參考下。2018-05-05
Android 高仿微信朋友圈動(dòng)態(tài)支持雙擊手勢(shì)放大并滑動(dòng)查看圖片效果
這篇文章主要介紹了Android 高仿微信朋友圈動(dòng)態(tài)支持雙擊手勢(shì)放大并滑動(dòng)查看圖片效果,需要的朋友參考下2017-01-01
解決android studio卡頓,提升studio運(yùn)行速度的方法
這篇文章主要介紹了解決android studio卡頓,提升studio運(yùn)行速度的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-03-03
Android ListView萬(wàn)能適配器實(shí)例代碼
本文主要介紹Android ListView萬(wàn)能適配器,這里整理了詳細(xì)的資料及實(shí)現(xiàn)代碼,以及實(shí)現(xiàn)效果圖,有需要的小伙伴可以參考下2016-09-09
Android StickListView實(shí)現(xiàn)懸停效果
這篇文章主要介紹了Android StickListView實(shí)現(xiàn)懸停效果的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-06-06
Android環(huán)形進(jìn)度條(安卓默認(rèn)形式)實(shí)例代碼
這篇文章主要介紹了Android環(huán)形進(jìn)度條(安卓默認(rèn)形式)實(shí)例代碼的相關(guān)資料,需要的朋友可以參考下2016-03-03
Android項(xiàng)目中g(shù)radle的執(zhí)行流程
大家好,本篇文章主要講的是Android項(xiàng)目中g(shù)radle的執(zhí)行流程,感興趣的同學(xué)趕快來(lái)看一看吧,對(duì)你有幫助的話記得收藏一下2022-01-01
Android異步回調(diào)中的UI同步性問(wèn)題分析
這篇文章主要為大家詳細(xì)分析了Android異步回調(diào)中的UI同步性問(wèn)題,感興趣的小伙伴們可以參考一下2016-06-06

