Hey all I've created Google's exmaple of a tab layout
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(开发者_StackOverflow).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);
}
I know how to create my own icon to use instead of their grey and white microphone, but I can't figure out how to make my custom icon fill the entire tab. When the tab is selected it's a light grey with my pic in the middle and when not selected its a dark grey with my pic in the middle. I prefer to have my own pic fill the entire tab, and for more knowledgeable; my own choice in color to fill the tab.
You can use setIndicator(View view)
to do more complex things with the Tab Indicator.
Try playing with this...
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);
I'm not sure if it's what you want but you can (in theory) use any View class as the Indicator if ImageView doesn't work for you.
EDIT:
I tried...
BitmapDrawable bmd = new BitmapDrawable();
bmd = (BitmapDrawable) res.getDrawable(R.drawable.ic_tab_artists_grey);
imgView.setImageDrawable(bmd);
精彩评论