简体   繁体   English

如何更新Tab Android中的指标图像?

[英]How to update indicator image in Tab android?

I have two tabs with two indicators and its working fine 我有两个选项卡,带有两个指示器,并且工作正常

tabHost = (FragmentTabHost) findViewById(android.R.id.tabhost);
    tabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent);

    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
        peopleTabSpec = tabHost.newTabSpec("people").setIndicator(null, getResources().getDrawable(R.drawable.group_icon));
        chatTabSpec = tabHost.newTabSpec("chat").setIndicator(null, getResources().getDrawable(R.drawable.icon_chat_green));
        tabHost.addTab(peopleTabSpec, MyTestFragment.class, null);
        tabHost.addTab(chatTabSpec, MyTestFragment2.class, null);
    } else {
        peopleTabSpec = tabHost.newTabSpec("people").setIndicator(null, getResources().getDrawable(R.drawable.group_icon, null));
        chatTabSpec = tabHost.newTabSpec("chat").setIndicator(null, getResources().getDrawable(R.drawable.icon_chat_green, null));
        tabHost.addTab(peopleTabSpec, MyTestFragment.class, null);
        tabHost.addTab(chatTabSpec, MyTestFragment2.class, null);
    }
    tabHost.getTabWidget().getChildAt(0).setBackgroundResource(R.drawable.tab_bg_selected);
    tabHost.getTabWidget().getChildAt(1).setBackgroundResource(R.drawable.tab_bg_unselected);

Now i need to change the indicator on tab change i have tried using setting the indicator again with using the method but it wont work any one help me 现在我需要更改选项卡更改上的指示器,我尝试使用方法再次设置指示器,但是任何人都无法帮助我

 @Override
public void onTabChanged(String s) {
    if (s.equals("people")) {
        tabHost.getTabWidget().getChildAt(0)
                .setBackgroundResource(R.drawable.tab_bg_selected);
        tabHost.getTabWidget().getChildAt(1)
                .setBackgroundResource(R.drawable.tab_bg_unselected);
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
            peopleTabSpec.setIndicator(null, getResources().getDrawable(R.drawable.group_icon));
            chatTabSpec.setIndicator(null, getResources().getDrawable(R.drawable.icon_chat_green));
        } else {
            peopleTabSpec.setIndicator(null, getResources().getDrawable(R.drawable.group_icon, null));
            chatTabSpec.setIndicator(null, getResources().getDrawable(R.drawable.icon_chat_green, null));
        }

    } else if (s.equals("chat")) {
        tabHost.getTabWidget().getChildAt(1)
                .setBackgroundResource(R.drawable.tab_bg_selected);
        tabHost.getTabWidget().getChildAt(0)
                .setBackgroundResource(R.drawable.tab_bg_unselected);
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
            peopleTabSpec.setIndicator(null, getResources().getDrawable(R.drawable.icon_group));
            chatTabSpec.setIndicator(null, getResources().getDrawable(R.drawable.icon_chat));
        } else {
            peopleTabSpec.setIndicator(null, getResources().getDrawable(R.drawable.icon_group, null));
            chatTabSpec.setIndicator(null, getResources().getDrawable(R.drawable.icon_chat, null));
        }


    }
    tabHost.getTabWidget().getChildAt(0).setTag(peopleTabSpec.getTag());
    tabHost.getTabWidget().getChildAt(1).setTag(chatTabSpec.getTag());

}

I tried using the above code but wont work ! 我尝试使用上面的代码,但无法正常工作!

 ImageView peopleIndicator = (ImageView) tabHost.getTabWidget().getChildAt(0).findViewById(android.R.id.icon);
            peopleIndicator.setImageDrawable(getResources().getDrawable(R.drawable.group_icon, null));
            ImageView chatIndicator = (ImageView) tabHost.getTabWidget().getChildAt(1).findViewById(android.R.id.icon);
            chatIndicator.setImageDrawable(getResources().getDrawable(R.drawable.icon_chat_green, null));

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

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