2012-11-13 30 views
0

我看到了這個問題here。解決的辦法是做一個XML的圖標,並在隨後getResources().getDrawable(R.drawable.tabicon); 所以這是我的java:標籤中的圖標(API 16+)

TabHost tabhost = (TabHost)findViewById(R.id.tabhost); 
    tabhost.setup(); 
    TabSpec tabspecs1 = th.newTabSpec("example"); 
    tabspecs1.setContent(R.id.tab1); 
    tabspecs1.setIndicator("Example"); 
    getResources().getDrawable(R.drawable.tabicon); 
    th.addTab(tabspecs1); 
    listView = getListView(); 

我tabicon.xml:

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
    <!-- When selected, use icon1 --> 
    <item android:drawable="@drawable/ic_action_line_chart" 
      android:state_selected="true" /> 
    <!-- When not selected, use icon2--> 
    <item android:drawable="@drawable/ic_action_calculator" /> 
</selector> 

線圖和計算器在繪製-folders。 我錯過了什麼?標籤工作正常,但沒有圖標...?


好,我這樣做是這樣的:

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_home); 
    setTitle("MyApp"); 
    listView = getListView(); 
    TabHost th = (TabHost)findViewById(R.id.tabhost); 
    th.setup(); 
    TabSpec tabspecs1 = th.newTabSpec("tag01"); 
    tabspecs1.setContent(R.id.tab1); 
    tabspecs1.setIndicator ("exlample"),getResources().getDrawable(R.drawable.tabicon)); 
    th.addTab(tabspecs1); 
    TabSpec tabspecs2 = th.newTabSpec("tag02"); 
    tabspecs2.setContent(R.id.tab2); 
    tabspecs2.setIndicator ("lululu", getResources().getDrawable(R.drawable.tabicon)); 
    th.addTab(tabspecs2); 
} 

我用tabicon.xml兩種,僅有4測試。

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
<!-- When selected, use icon1 --> 
<item android:drawable="@drawable/ic_icon1" 
     android:state_selected="true" /> 
<!-- When not selected, use icon2--> 
<item android:drawable="@drawable/ic_icon2" /> 
</selector> 

回答

1

你得到的是drawable,但沒有設置它。試試這個方法:

http://developer.android.com/reference/android/widget/TabHost.TabSpec.html#setIndicator(java.lang.CharSequence,android.graphics.drawable.Drawable)

setIndicator ("Example", getResources().getDrawable(R.drawable.tabicon)) 
+0

我用tabspecs1.setIndicator(blahblah),但它仍然沒有顯示我的圖標...... – Lesik2008

+0

?你知道該怎麼辦? – Lesik2008

+0

與你所作的更改共享更多代碼... –

0

getResources().getDrawable(R.drawable.tabicon); 

沒有做任何事情。您不在任何地方設置檢索到的Drawable。