使用自定义图片和/或颜色填充 Android 选项卡

发布于 2024-10-11 03:06:52 字数 1619 浏览 2 评论 0原文

大家好,我已经创建了 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

仄言 2024-10-18 03:06:52

您可以使用 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);

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);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文