java - 选项卡布局不会显示选项卡图标

标签 java android eclipse

所以我终于为我的应用程序制作了一个可以完美运行的选项卡布局,但缺少一些东西。我也花了很多精力设计图标,但我注意到它们根本没有显示。我将发布下面的代码来展示我是如何尝试做到这一点的。

整个java代码(这都是在onCreate中):

TabHost tabHost = getTabHost();

    //CRAFTING TAB
    TabSpec craftTabSpec = tabHost.newTabSpec("Crafting");
    craftTabSpec.setIndicator("Crafting", getResources().getDrawable(R.drawable.crafticonstate));
    Intent craftIntent = new Intent(this, Bifrost.class);
    craftTabSpec.setContent(craftIntent);

    //ADDITION INFO TAB
    TabSpec infoTabSpec = tabHost.newTabSpec("Info");
    infoTabSpec.setIndicator("Info", getResources().getDrawable(R.drawable.infoiconstate));
    Intent infoIntent = new Intent(this, Bifrostinfo.class);
    infoTabSpec.setContent(infoIntent);

    tabHost.addTab(craftTabSpec);
    tabHost.addTab(infoTabSpec);

crafticonstate XML 代码:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <!-- When selected, use grey -->
    <item android:drawable="@drawable/weaponsmith_logo_hover"
      android:state_selected="true" />
    <!-- When not selected, use white-->
    <item android:drawable="@drawable/weaponsmith_logo" />
</selector>

我还尝试在没有 xml 文件的情况下添加图标,就像“R.drawable.icon”一样,但这也不起作用。

最佳答案

这是我用过的一个很好的技巧。

不幸的是,由于 TabHost 已被折旧,它以某种方式仅在更高级别的 Api 上的屏幕上显示文本。一些解决方法是这样做:

   //CRAFTING TAB
    TabSpec craftTabSpec = tabHost.newTabSpec("Crafting");      
    craftTabSpec.setIndicator("",getResources().getDrawable(R.drawable.crafticonstate));
    Intent craftIntent = new Intent(this, Bifrost.class);
    craftTabSpec.setContent(craftIntent);

删除指示符(文本),强制选项卡加载图像。如果您还想要文字。您可以修改图像以包含文本。

希望有帮助

关于java - 选项卡布局不会显示选项卡图标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17665232/

相关文章:

java - 新建 Java 项目向导中缺少内容部分

android - 在 performclick 上设置微调值位置

具有多个按钮的android小部件

java - 返回列表中的项目数

java - 静态 block java中的常量问题

java - Android 不使用 listFiles() 方法列出目录中的所有文件

java - Eclipse RCP : Command handler for view and editor

eclipse - 带语法错误的注释行

java - 如何在我的 JSP 页面中访问 JSTL 核心设置的变量?

java - Spring MVC :resources needs to map to the same filename?