使用自定义图片和/或颜色填充 Android 选项卡
大家好,我已经创建了 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);
}
我知道如何创建自己的图标来代替他们的灰色和白色麦克风,但我不知道如何让我的自定义图标填充整个选项卡。当选择该选项卡时,它是浅灰色的,我的照片在中间;当未选择该选项卡时,它是深灰色的,我的照片在中间。我更喜欢让我自己的图片填满整个选项卡,这样可以让我更有知识;我自己选择的颜色来填充选项卡。
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().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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用
setIndicator(View view)
使用选项卡指示器执行更复杂的操作。尝试使用这个...
我不确定这是否是您想要的,但如果 ImageView 不适合您,您可以(理论上)使用任何 View 类作为指示器。
编辑:
我尝试过...
You can use
setIndicator(View view)
to do more complex things with the Tab Indicator.Try playing with this...
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...