最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Android TabHost選項卡標簽圖標始終不出現(xiàn)的解決方法

 更新時間:2019年03月16日 10:07:45   作者:張智清  
這篇文章主要介紹了Android TabHost選項卡標簽圖標始終不出現(xiàn)的解決方法,涉及Android界面布局相關(guān)屬性與狀態(tài)設(shè)置操作技巧,需要的朋友可以參考下

本文實例分析了Android TabHost選項卡標簽圖標始終不出現(xiàn)的解決方法。分享給大家供大家參考,具體如下:

在學習Android TabHost布局過程中,很多教程告訴我,這樣來顯示選項卡標簽的圖標和文字:

TapSpec spec1 = tabHost.newTabSpec("tab 1");
spec1.setIndicator("選項卡一", getResources().getDrawable(R.drawable.tab_icon));
spec1.setContent(R.id.tab1);
tabHost.addTab(spec1);

折騰來折騰去,setIndicator(label, drawable)這個方法始終不能將標題文字與圖標一起顯示出來,只有文字標題。

在沒將電腦砸了之前,通過萬能的stackoverflow.com終于知道確切答案以及相應(yīng)方法了:
http://stackoverflow.com/questions/10745092/icon-in-tab-is-not-showing-up

其實就是SDK 4.03(冰激凌)下:只有文字標題顯示,圖標是不顯示的。如果將文字標題設(shè)置為空字符串,則此時圖標可顯示。

對于冰激凌下兩全其美的方法,只能是自定義標簽卡布局,創(chuàng)建一個包含ImageView和TextView組件的界面布局文件 tab_indicator.xml(layout/tab_indicator.xml),然后用setIndicator(View view)方法來設(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)建一個tab_info.xml文件,用來指示Tab圖標的各狀態(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)在就可以通過下面的代碼將我們自定義的視圖作為一個indicator配置給TapSpec對象。

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);

注意:當用自定義視圖的indicator來添加Tab時,要將strip_enabled屬性設(shè)置為false。若要兼顧底部strip,那在添加最后一個Tab后設(shè)置getTabWidget().setStripEnabled(true);

更多關(guān)于Android相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Android開發(fā)入門與進階教程》、《Android調(diào)試技巧與常見問題解決方法匯總》、《Android基本組件用法總結(jié)》、《Android視圖View技巧總結(jié)》、《Android布局layout技巧總結(jié)》及《Android控件用法總結(jié)

希望本文所述對大家Android程序設(shè)計有所幫助。

相關(guān)文章

最新評論

神池县| 正宁县| 遂平县| 安国市| 溧水县| 阳江市| 汽车| 安新县| 普兰店市| 祁东县| 郑州市| 临清市| 来宾市| 嘉黎县| 措美县| 西吉县| 舒城县| 伊吾县| 蓝山县| 陆河县| 宁城县| 金湖县| 谷城县| 通城县| 木兰县| 武邑县| 新源县| 房产| 河东区| 宣化县| 健康| 房产| 靖边县| 伊川县| 伊金霍洛旗| 淮滨县| 洞口县| 介休市| 广州市| 汽车| 涟源市|