1

StyleMe's image is style_link如何更改標籤指示圖像的背景?

這是一張圖片。 我希望tabview圖像背景是這樣的。當它沒有被選中時,它顯示白色圖像,當它被選中時,它顯示綠色圖像。

下面是代碼:

Resources resources = getResources(); 
TabHost tabhost = getTabHost(); 
Intent one = new Intent().setClass(this, StyleMe.class); 
TabSpec tb1=tabhost.newTabSpec("One").setIndicator("",resources.getDrawable 
(R.drawable.style_link)).setContent(one); 
tabhost.addTab(tb1); 

回答

0

更好的解決辦法是使用背景選擇,這樣你就不必檢查onTabChanged並做手動更新:

private void initTabsAppearance(TabWidget tabWidget) { 
    // Change background 
    for(int i=0; i < tabWidget.getChildCount(); i++) 
     tabWidget.getChildAt(i).setBackgroundResource(R.drawable.tab_bg); 
} 

凡tab_bg是一個帶有選擇器的xml drawable:

<selector xmlns:android="http://schemas.android.com/apk/res/android">  
    <item android:state_selected="true" android:drawable="@drawable/tab_bg_selected" /> 
    <item android:drawable="@drawable/tab_bg_normal" /> 
</selector> 

希望這會有所幫助。