如何更改 TabHost 中的选项卡图像 [英] How to change the Tabs Images in the TabHost
问题描述
我在我的应用程序中使用 TabHost,我在我的应用程序中使用了四个选项卡,我想在选择了特定选项卡但未选择时在 TabHost 中使用不同的图像.我需要为每个特定选项卡使用不同的图像.
I am using the TabHost in my application, I am using four Tabs in my application and I want to use the different Images in the TabHost when the Particular Tab is been Selected and not selected. I need to use to different Images for a particular tab each.
当我选择任何标签时,图像有点亮,当我切换到另一个标签时,明亮的图像变成灰色阴影.
When I Select any Tab the Image is little bright and when I switch to another Tab that bright Image becomes grey shaded.
我已经实现了 TabHost,但我不知道如何更改 TabHost 中的图像.
I have implemented the TabHost but I don know how to change the Images in the TabHost.
有人能帮我吗.
谢谢,大卫
推荐答案
如果您希望对选中状态和未选中状态使用不同的图像,请在您的 drawables 文件夹中为每个选项卡创建选择器"XML 文件,例如tab1_selector.xml, tab2_selector.xml 应包含以下内容,替换选定和未选定状态对图像的可绘制引用.即
If you wish to use different images for the selected and unselected states, then create 'selector' XML files in your drawables folder for each tab, e.g. tab1_selector.xml, tab2_selector.xml which should contain the following, replacing the drawable references to your images for selected and unselected states. i.e.
<?xml version="1.0" encoding="utf-8"?>
<selector
xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_selected="true"
android:drawable="@drawable/tab1_selected_image" />
<item
android:state_selected="false"
android:drawable="@drawable/tab2_unselected_image" />
</selector>
然后使用 bharath 上面写的 .setIndicator 方法,您应该引用新的 xml drawable 资源.
Then using the .setIndicator method as bharath wrote above you should reference your new xml drawable resource.
这篇关于如何更改 TabHost 中的选项卡图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!