国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Android > 正文

Android TabHost選項卡標簽圖標始終不出現的解決方法

2019-10-21 21:19:16
字體:
來源:轉載
供稿:網友

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

在學習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終于知道確切答案以及相應方法了:
http://stackoverflow.com/questions/10745092/icon-in-tab-is-not-showing-up

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

對于冰激凌下兩全其美的方法,只能是自定義標簽卡布局,創建一個包含ImageView和TextView組件的界面布局文件 tab_indicator.xml(layout/tab_indicator.xml),然后用setIndicator(View view)方法來設置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圖片資源目錄下創建一個tab_info.xml文件,用來指示Tab圖標的各狀態。

<?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>

現在就可以通過下面的代碼將我們自定義的視圖作為一個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 style="margin: 0px; padding: 0px; outline: none; line-height: 25.2px; font-size: 14px; width: 660px; overflow: hidden; clear: both; font-family: tahoma, arial, "Microsoft YaHei";">	
tabHost = getTabHost(); //tabHost is a private fieldaddTab("First", R.drawable.tab_info);addTab("Second", R.drawable.tab_info);addTab("Third", R.drawable.tab_info);

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

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


注:相關教程知識閱讀請移步到Android開發頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 罗平县| 紫金县| 双鸭山市| 五寨县| 阳春市| 天门市| 修水县| 穆棱市| 扶余县| 贵定县| 滨海县| 托里县| 莫力| 门头沟区| 尚志市| 东海县| 涞源县| 桐城市| 新巴尔虎左旗| 延长县| 通州市| 韩城市| 弋阳县| 临武县| 车致| 浠水县| 南川市| 阿克陶县| 大埔县| 株洲县| 陇川县| 富平县| 古丈县| 章丘市| 顺义区| 潼南县| 叶城县| 凤台县| 大英县| 张北县| 彩票|