【问题标题】:How to update specific tab icon in tabhost android?如何更新tabhost android中的特定标签图标?
【发布时间】:2017-09-15 07:25:32
【问题描述】:

我有 4 个标签,其中一个是通知图标。默认情况下使用选项卡规范设置所有图标。

spec = host.newTabSpec("Tab Four");
                        spec.setContent(R.id.tab5);
                        spec.setContent(new Intent(getApplicationContext(), ICNotificationActivity.class));
                            spec.setIndicator("", getResources().getDrawable(R.drawable.tab_notification_noted));
host.addTab(spec);

延迟后,我必须用另一个资源文件更新选项卡 4 图标。如何更新标签主机?有没有类似 host.addTab(spec) 的更新功能?

【问题讨论】:

    标签: android android-tabhost


    【解决方案1】:

    我在这里循环所有选项卡并更改所选选项卡的颜色和图标。 在 for 循环中,我首先将所有选项卡设置为未选中,而不是仅将选定选项卡设置为更新的图标和颜色。

    private void setTabColor(TabHost tabhost, int position) {
        for (int i = 0; i < tabhost.getTabWidget().getChildCount(); i++) {
            //      unselected
            tabhost.getTabWidget().getChildAt(i)
                    .setBackgroundResource(R.drawable.tab_unselected);
    
            TextView tv = (TextView) tabHost.getTabWidget().getChildAt(i).findViewById(R.id.tvTabTitle);
            tv.setTextColor(getResources().getColor(R.color.text_white));
        }
        if (position > 0) {
            //      selected
            tabhost.getTabWidget().setCurrentTab(position);
            tabhost.getTabWidget().getChildAt(tabhost.getCurrentTab())
                    .setBackgroundResource(R.drawable.tab_selected);
            TextView tv = (TextView) tabHost.getTabWidget().getChildAt(tabhost.getCurrentTab()).findViewById(R.id.tvTabTitle);
            tv.setTextColor(getResources().getColor(R.color.tab_color));
        }
    
    }
    

    你可以使用上面的方法调用

        new Handler(Looper.getMainLooper()).postDelayed(new Runnable() {
        @Override
        public void run() {
            //Do something here
         setTabColor(myTabHost, myTabPosition);
        }
    }, 5000);
    

    【讨论】:

    • java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法“void android.view.View.setBackgroundResource(int)”。显示此日志错误。 { 主机 = (TabHost) findViewById(R.id.tabHost);主机设置(); host.getTabWidget().setStripEnabled(false); host.getTabWidget().setDividerDrawable(R.drawable.border_tab_activity); host.setup(this.getLocalActivityManager());} ,已经定义了tabhost。
    • @SARATHV 我已经给出了我的代码,您需要根据需要进行更改。
    • 代码工作正常,谢谢。我在制表位上犯了错误,这就是为什么我得到空指针异常
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-11
    相关资源
    最近更新 更多