gpt4 book ai didi

java - 更改 Tabhost Android 上的图标

转载 作者:行者123 更新时间:2023-12-01 14:36:06 24 4
gpt4 key购买 nike

我想在点击 tabhost 时更改图标

下面是源码

    private void setTabs() {
addTab("Home", TabHome.class, R.drawable.home);
addTab("Performers", TabPerformers.class, R.drawable.performers);
addTab("Tickets", TabTickets.class, R.drawable.tickets);
addTab("Info", TabInfo.class, R.drawable.info);

}

private void addTab(String labelId, Class<?> c, int drawableId) {
tabHost = getTabHost();
Intent intent = new Intent(this, c);
TabHost.TabSpec spec = tabHost.newTabSpec("tab" + labelId);

tabIndicator = LayoutInflater.from(this).inflate(R.layout.tab_indicator, getTabWidget(), false);
icon = (ImageView) tabIndicator.findViewById(R.id.icon);
icon.setImageResource(drawableId);
spec.setIndicator(tabIndicator);
spec.setContent(intent);

tabHost.addTab(spec);
}

当用户按下我在 tabhost 事件下面使用的选项卡时,我想更改选项卡的图标

   tabHost.setOnTabChangedListener(new OnTabChangeListener() {

@Override
public void onTabChanged(String tabId) {

if (tabId.equals("tabHome")) {

但未能成功

还有一个 tab_indicater.xml 文件,但只有背景会发生变化,而不是图标

下面是xml代码

   <?xml version="1.0" encoding="utf-8"?>
<!-- Non focused states -->
<item android:drawable="@drawable/tab_unselected" android:state_focused="false" android:state_pressed="false" android:state_selected="false"/>
<item android:drawable="@drawable/tab_bg_selector" android:state_focused="false" android:state_pressed="false" android:state_selected="true"/>

<!-- Focused states -->
<item android:drawable="@drawable/tab_bg_selector" android:state_focused="true" android:state_pressed="false" android:state_selected="false"/>
<item android:drawable="@drawable/tab_bg_selector" android:state_focused="true" android:state_pressed="false" android:state_selected="true"/>

<!-- Pressed -->
<item android:drawable="@drawable/tab_bg_selector" android:state_pressed="true" android:state_selected="true"/>
<item android:drawable="@drawable/tab_press" android:state_pressed="true"/>

下面是屏幕截图

enter image description here

当我们点击任何一个选项卡时,它的图标应该发生变化,就像这里它必须变成橙色..

任何人都可以帮助我吗...

最佳答案

没有直接的方法来更改 TabSpec 图标。您必须将图标放入选择器可绘制对象中。

关于java - 更改 Tabhost Android 上的图标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16477188/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com