繁体   English   中英

更改Tabhost Android上的图标

[英]change icon on tabhost android

我想在点击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事件下方使用的Tab键时更改Tab键的图标

   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"/>

下面是屏幕截图

在此处输入图片说明

当我们单击任何一个选项卡时,它的图标应该更改,就像这里必须变成橙色一样。

有谁能够帮助我...

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

暂无
暂无

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

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