标签布局将不会显示选项卡图标 [英] Tab layout won't show tab icons
问题描述
所以,我终于为我的应用程序,完美的作品标签布局,但有一些缺失。我也投入了大量精力在设计图标,但我发现他们完全无法显示。我会后下面的code,以显示我如何试图做到这一点。
So I finally made a tab layout for my app that works flawlessly, but there's something missing. I also put a lot of effort in designing icons but I noticed that they're not showing at all. I'll post the code below to show how I tried to do it.
整个Java的code(这是所有withing的onCreate):
The whole java code (this is all withing 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 code:
crafticonstate XML code:
<?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',但这不能工作。
I also tried to add icon without the xml file, simply like 'R.drawable.icon' but that doesn't work either.
推荐答案
这是我使用的一个很好的伎俩。
This is one nice trick that I used.
不幸的是,因为TabHost是pciated德$ P $,它在某种程度上只显示在更高级别的API屏幕上的文本。有些解决方法是做到这一点:
Unfortunately since TabHost is depreciated, it somehow only show text in the screen on higher level Api. Some workaround is to do this:
//CRAFTING TAB
TabSpec craftTabSpec = tabHost.newTabSpec("Crafting");
craftTabSpec.setIndicator("",getResources().getDrawable(R.drawable.crafticonstate));
Intent craftIntent = new Intent(this, Bifrost.class);
craftTabSpec.setContent(craftIntent);
删除指示器(文本),强制标签加载图像。如果你还是想要的文字。你可以修改你的图像包含的文本。
Deleting the indicator(text), force the tab to load the images. if you still want the text. you can modify your image to include the text.
希望帮助
这篇关于标签布局将不会显示选项卡图标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!