如何更改 TabHost 中的选项卡图像
我在应用程序中使用 TabHost,在应用程序中使用四个选项卡,并且当选择特定选项卡和未选择特定选项卡时,我想在 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.
I have implemented the TabHost but I don know how to change the Images in the TabHost.
Can anybody help me in this.
Thanks,
david
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
如果您希望对选定和未选定状态使用不同的图像,请在每个选项卡的可绘制文件夹中创建“选择器”XML 文件,例如 tab1_selector.xml、tab2_selector.xml,其中应包含以下内容,替换对图像的可绘制引用对于选定和未选定的状态。即
然后使用 .setIndicator 方法(如 bharath 上面所写),您应该引用新的 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.
Then using the .setIndicator method as bharath wrote above you should reference your new xml drawable resource.
首先,您必须拥有两个图像,因为您想要从一个图像更改为另一个图像,所以您需要两个图像,并且必须将其放在三个可绘制文件夹中。
在我的示例中,我必须使用图像,其中一个名为 icon1.png 和 icon2.png。
之后,在可绘制文件夹内创建一个 xml 文件(所有可绘制文件夹的文件相同)。这是文件:
您可以选择选择选项卡时显示的图像。在这种情况下,icon1将会出现,因为我们在state_selected=true的标签上声明了它。
现在,您在三个可绘制文件夹中拥有两个图像和 xml 文件。好的!
现在,在声明选项卡的类中,为要添加的每个选项卡添加此行。
请记住,R.drawable.yourxmlfile 对应于您在可绘制文件夹中创建的 xml 文件。
就是这样!希望这对您有帮助。
First of all you must have the two images, because you want to change from one to another, so you need the both images, and you must place it on the three drawable folders.
In my example I have to images, one called icon1.png and icon2.png.
After that, create a xml file inside the drawable folders (the same file for all drawable folders). This is the file:
You can choose what image is the one which will appear when the tab is selected. In this case, the icon1 will appear, cause we declared it on the tag where state_selected=true.
So now, you have the two images and the xml file inside the three drawable folders. Ok!
Now, in the class you declare the tabs, add this line for each tab you want to add.
Remember that R.drawable.yourxmlfile correponds to the xml file you created in the drawable folders.
That's it! Hope this helps you.
设置文本和icon 我们需要使用 setIndicator 属性。
使用它为每个选项卡设置单独的图像
To set text & icon we need to use setIndicator property.
use this to set seperate image for each tab
创建一个选择器 xml 文件 tabicon.xml 并放入此代码
现在转到您的 TabActivity 并放入此代码
Create a selector xml file tabicon.xml and put this code
now go to your TabActivity and put this code
在 这个 TabLayout 教程中,当 Tab 被使用时,会使用不同的图像已选择和未选择。
基本上你必须创建一个 Statelist 可绘制对象。这是来自开发人员网站的相同代码,
还调用 setIndicator(CharSequence, Drawable) 来设置选项卡的文本和图标。
In this TabLayout tutorial, different images are used when a Tab is selected and not selected.
Basically you have to create a Statelist drawable. Here's the code for the same from the developer site
Also setIndicator(CharSequence, Drawable) is called to set the text and icon for the tab.
此代码显示如何在选项卡主机中设置图标以及设置意图
this codes show how to set an icon in tab host and also setting intent
您可以使用 ImageButton ,它更好,因为 imageView 可以选择和不选择,并且 ImageButton 可以选择不选择和按下等等......
You can to use ImageButton it's better because a imageView can be selected and not selected and the ImageButton can be selected not selected and pressed and others....
@Suchismita 最好使用 TextView 而不是 TabActivity。
我在选项卡活动中遇到以下问题
我无法在同一选项卡中启动另一个活动,这是我面临的主要问题
下一步正在自定义选项卡视图,我无法更改可绘制的分隔线。
Next 中已弃用,使用 TextView 我发现它非常容易处理事件和活动流,在这里您可以完全控制应用程序的行为,还可以根据需要自定义选项卡的外观和感觉。
您对如何实施感兴趣吗?
@Suchismita better you use TextView instead of TabActivity.
I faced these following problems in tabactivity
I could not start another activity within same tab, this is major problem i faced
next is customizing view of tab, I could not change divider drawable.
Next using TextView I found its very easy to handle events and activity flow,here You have full control on the behavior of application and also You can customize the look and feel of tab however you want.
are you interested in how to implement ?
如果您想以编程方式更改选项卡的图像,则:
If you want to change image of tab programmatically then: