2011-01-07 68 views
3

嘿,所有我創建了一個tab layout填寫的Android標籤定製PIC和/或顏色

HelloTabWidget.java谷歌的〔實施例:

public void onCreate(Bundle savedInstanceState) { 
super.onCreate(savedInstanceState); 
setContentView(R.layout.main); 

Resources res = getResources(); // Resource object to get Drawables 
TabHost tabHost = getTabHost(); // The activity TabHost 
TabHost.TabSpec spec; // Resusable TabSpec for each tab 
Intent intent; // Reusable Intent for each tab 

// Create an Intent to launch an Activity for the tab (to be reused) 
intent = new Intent().setClass(this, ArtistsActivity.class); 

// Initialize a TabSpec for each tab and add it to the TabHost 
spec = tabHost.newTabSpec("artists").setIndicator("Artists", 
        res.getDrawable(R.drawable.ic_tab_artists)) 
       .setContent(intent); 
tabHost.addTab(spec); 

// Do the same for the other tabs 
intent = new Intent().setClass(this, AlbumsActivity.class); 
spec = tabHost.newTabSpec("albums").setIndicator("Albums", 
        res.getDrawable(R.drawable.ic_tab_albums)) 
       .setContent(intent); 
tabHost.addTab(spec); 

intent = new Intent().setClass(this, SongsActivity.class); 
spec = tabHost.newTabSpec("songs").setIndicator("Songs", 
        res.getDrawable(R.drawable.ic_tab_songs)) 
       .setContent(intent); 
tabHost.addTab(spec); 

tabHost.setCurrentTab(2); 

}

我知道如何創建我的自己的圖標,而不是他們的灰色和白色的麥克風,但我無法弄清楚如何讓我的自定義圖標填充整個選項卡。當選中此選項卡時,它是淺灰色,中間是我的圖片,未選中深灰色,中間是我的圖片。我更喜歡用我自己的照片填滿整個標籤,並且更加知識淵博;我自己選擇用顏色來填充標籤。

+0

這事我真的不喜歡關於Android。標準標籤根本不漂亮,如果選中,則無法再閱讀文本。像你想要做的那樣創建一個自定義的TabHost是非常困難的。 – 2011-01-07 22:38:31

回答

3

您可以使用setIndicator(View view)通過選項卡指示器執行更復雜的操作。

嘗試玩這個......

ImageView imgView = new ImageView(this); 

// Use imgView.setImageDrawable(Drawable drawable) or 
// imgView.setImageBitmap(Bitmap bitmap) to load 
// the image you want into imgView 

spec = tabHost.newTabSpec("artists").setIndicator(imgView).setContent(intent); 

我不知道這是否是你想要的,但你可以(在理論上)使用任何View類的指標,如果ImageView的不爲你工作。

編輯:

我想...

BitmapDrawable bmd = new BitmapDrawable(); 
    bmd = (BitmapDrawable) res.getDrawable(R.drawable.ic_tab_artists_grey); 
    imgView.setImageDrawable(bmd); 
+0

我給了這個鏡頭,雖然arists標籤變成了完全黑色。 HelloTabWidget中的aritsts行如下所示: spec = tabHost.newTabSpec(「artists」).setIndicator(imgView).setContent(intent); \t tabHost.addTab(spec); 不知道如果這是正確的,如果是的話如何引用圖像? – Clozecall 2011-01-08 00:38:27