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