简体   繁体   English

Android Tabactivity-如何在选择标签时更改图标图像

[英]Android tabactivity - how to change icon image on selecting a tab

In my tabactivity, I display icon that I get from the database, by creating an ImageView and passsing this to the setIndicator method. 在我的Tabactivity中,通过创建ImageView并将其传递给setIndicator方法,显示从数据库中获取的图标。 I have only one image for each tab, so I was wondering if there is way to just change the color of the image alone (as opposed to changing the tab's color) when a tab is selected? 每个选项卡只有一个图像,所以我想知道选择选项卡时是否有办法仅更改图像的颜色(而不是更改选项卡的颜色)? Also is it possible to hide the gray line that is being drawn at the bottom of the tabs when a tab is selected? 选择选项卡时,是否还可以隐藏在选项卡底部绘制的灰线? Any help is appreciated..thanks 任何帮助表示赞赏..谢谢

Hiding gray line: 隐藏灰线:

yourTabWidget.setStripEnabled(false);

Setting custom image per tab state: 设置每个标签状态的自定义图像:

tabitemicon.xml: tabitemicon.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/orange"
          android:state_selected="true" />
    <!-- When not selected, use white-->
    <item android:drawable="@drawable/white" />
</selector>

Java code: Java代码:

tabHost = getTabHost():
tabHost.newTabSpec("test").setIndicator("My Tab Title",
                          res.getDrawable(R.drawable.tabitemicon))
                      .setContent(someIntent);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM