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 - 使用 Docker 设置 Java 开发环境

java - hibernate 查询

Java String.replaceAll() 正则表达式的行为不符合预期,我做错了什么?

java - 登录在我的 servlet 程序中不起作用

android - 如何为最终版本 apk 设置我的 gradle

java - Eclipse中的Kotlin和Java项目,NoClassDefFoundError,类文件在哪里?

使用 JAXB 将 Java 转换为 XML

android - 最后一行可扩展 ListView 在 android 中不是完整 View

android - 在 Android canvas 中绘制 2D 曲线?

java - 使用 JDT 搜索引擎按名称查找 IProject 中的 TypeDeclaration