3

Googleのタブレイアウトの例を作成しました

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);

}

灰色と白のマイクの代わりに使用する独自のアイコンを作成する方法は知っていますが、カスタムアイコンをタブ全体に表示する方法がわかりません。タブが選択されている場合は、私の写真が中央にある明るい灰色であり、選択されていない場合は、私の写真が中央にある暗い灰色です。私は自分の写真をタブ全体に塗りつぶしてもらいたいと思っています。タブを埋めるための色の私自身の選択。

4

1 に答える 1

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);

それがあなたの望むものかどうかはわかりませんが、ImageViewが機能しない場合は、(理論的には)任意のViewクラスをインジケーターとして使用できます。

編集:

私は試した...

    BitmapDrawable bmd = new BitmapDrawable();
    bmd = (BitmapDrawable) res.getDrawable(R.drawable.ic_tab_artists_grey);
    imgView.setImageDrawable(bmd);
于 2011-01-08T00:29:57.783 に答える