簡體   English   中英

如何更新Tab Android中的指標圖像?

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

我有兩個選項卡,帶有兩個指示器,並且工作正常

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);

現在我需要更改選項卡更改上的指示器,我嘗試使用方法再次設置指示器,但是任何人都無法幫助我

 @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());

}

我嘗試使用上面的代碼,但無法正常工作!

 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