image2.png   

這裡使用了類似這樣的代碼:
 
tabHost.addTab(tabHost.newTabSpec("landscapes").setIndicator("風景",
getResources().getDrawable(R.drawable.a2))


 
其中a2是放置在res/drawable目錄下的圖形檔,比如a2.gif。
 
如果想要更複雜的佈局,比如圖片和文字是在同一行的,那麼,就需要用到這個方法:
 
setIndicator(View view)
 
這樣問題就複雜了,需要自訂佈局,背景和格式,不過也不算難實現。
 
那麼,如果想選擇的tab圖示是彩色的,未選中的圖示是灰色的呢?也就是說想讓圖示是變化的。這就難辦了。因為android本身沒有提供這樣的api調用。
 
如果想做的話,就需要用蠻力了。可以參考這個帖子:
 
http://stackoverflow.com/questions/36881/updating-android-tab-icons
 
裡面提供了這樣的代碼,據說是可行的:
 
tabHost.setOnTabChangedListener(new OnTabChangeListener() {
public void onTabChanged(String tabId) {
if (TAB_MAP.equals(tabId)) {
ImageView iv = (ImageView)tabHost.getTabWidget().getChildAt(0).findViewById(android.R.id.icon);
iv.setImageDrawable(getResources().getDrawable(R.drawable.tab_map_black));
iv = (ImageView)tabHost.getTabWidget().getChildAt(1).findViewById(android.R.id.icon);
iv.setImageDrawable(getResources().getDrawable(R.drawable.tab_list_white));
} else if (TAB_LIST.equals(tabId)) {
ImageView iv = (ImageView)tabHost.getTabWidget().getChildAt(0).findViewById(android.R.id.icon);
iv.setImageDrawable(getResources().getDrawable(R.drawable.tab_map_white));
iv = (ImageView)tabHost.getTabWidget().getChildAt(1).findViewById(android.R.id.icon);
iv.setImageDrawable(getResources().getDrawable(R.drawable.tab_list_black));
}
}
});
arrow
arrow
    全站熱搜

    戮克 發表在 痞客邦 留言(0) 人氣()