Tabhost- Android ViewBadger徽章问题

时间:2014-10-16 11:02:58

标签: java android

在我的应用程序中,我想在一个标签上显示徽章。为此,我使用了android-viewbadger.jar文件Android ViewBadger我在tab-2上设置我的徽章意味着通知选项卡 我正在设置这样的徽章

    View target = findViewById(R.id.tab2); 
    badge1 = new BadgeView(this, target);
    badge1.setText("0");
    badge1.setBadgePosition(BadgeView.POSITION_BOTTOM_RIGHT);
    badge1.toggle();

当我设置我的徽章时,它会像这样显示在右侧 AVD sampleImage

我还尝试更改标签号

View target = findViewById(R.id.tab1);
View target = findViewById(R.id.tab2);
View target = findViewById(R.id.tab3); 
View target = findViewById(R.id.tab4);  

但它始终显示在右侧 以下是我正在使用的代码

@Override
protected void onCreate(Bundle arg0) {
    // TODO Auto-generated method stub
    getActionBar().hide();
    super.onCreate(arg0);
    setContentView(R.layout.fr_tabs);
    mainactivity_context = MainActivity.this;

    mtabhost=(TabHost)findViewById(android.R.id.tabhost);

    mtabhost.setup();/// it starts the tabs
    mtabhost.setOnTabChangedListener(this);


    mtabhost.addTab(newTab(TAB_HOME,R.string.Home,R.id.tab1,R.drawable.people_icon_dark));
    mtabhost.addTab(newTab(TAB_INBOX, R.string.Inbox, R.id.tab2, R.drawable.main_profile_icon));        
    mtabhost.addTab(newTab(TAB_Profile, R.string.Profile, R.id.tab3, R.drawable.notification_icon_ark));
    mtabhost.addTab(newTab(TAB_Setting, R.string.Setting, R.id.tab4, R.drawable.messages_icon_dark));
    mtabhost.addTab(newTab(TAB_VIDEOS, R.string.Video, R.id.tab5, R.drawable.more_icon_dark));
}


private TabSpec newTab(String tag, int label_id, int tab_content_id, int res)

{
    // TODO Auto-generated method stub
    View indicator=LayoutInflater.from(this).inflate(R.layout.tab_sample, null,false);

     ((TextView) indicator.findViewById(R.id.tex)).setText(label_id);            
     ((ImageView) indicator.findViewById(R.id.img)).setImageResource(res);

      ///we will use tabspec 
        TabSpec tabSpec=mtabhost.newTabSpec(tag);
        tabSpec.setIndicator(indicator);
        tabSpec.setContent(tab_content_id);

        View target = findViewById(R.id.tab2); 
        badge1 = new BadgeView(this, target);
        badge1.setText("0");
        badge1.setBadgePosition(BadgeView.POSITION_BOTTOM_RIGHT);
        badge1.toggle();

        return tabSpec;
}

@Override
public void onTabChanged(String tabId) {
    // TODO Auto-generated method stub

    if(TAB_HOME.equals(tabId)){
        People Frg=new People();
        Bundle b=new Bundle();
        b.putString("tag", tabId);

        Frg.setArguments(b);

        updateTab(Frg,R.id.tab1,true);
        current_tab=1;
        return;
    }

    if(TAB_INBOX.equals(tabId)){
        Profile Frg=new Profile();
        Bundle b=new Bundle();
        b.putString("tag", tabId);

        Frg.setArguments(b);

        updateTab(Frg,R.id.tab2,true);
        current_tab=1;          
        return;
    }

    if(TAB_VIDEOS.equals(tabId)){
        More Frg=new More();
        Bundle b=new Bundle();
        b.putString("tag", tabId);

        Frg.setArguments(b);

        updateTab(Frg,R.id.tab5,true);
        current_tab=4;
        return;
    }

    if(TAB_Profile.equals(tabId)){
        Intent intent = new  Intent(this, Notifications.class);
        startActivity(intent);
    }       
 }

    private void updateTab(Fragment frg,
        int place_holder, boolean addTobackStack) 
{
    // TODO Auto-generated method stub
    FragmentTransaction ft=getFragmentManager().beginTransaction();

    ft.replace(place_holder, frg);
    if(addTobackStack)
        ft.addToBackStack(null);
        ft.commit();
}


 public void launchNewFragment(Fragment frg1, int containerId) {

     updateTab(frg1, containerId,true);
     }
 public void launchfinishfragment(Fragment frg1, int containerId,boolean b) {         
     updateTab(frg1, containerId,true);
     }
}

检查以启用“布局绑定”选项 设置=>开发者选项=>布局绑定 enter image description here

当我使用public View getChildTabViewAt(int index)时;来自TabWidget Documentation

的方法
        View target = findViewById(R.id.tab2);
        target = mtabhost.getTabWidget().getChildTabViewAt(2); 

 my output will be this 

enter image description here

0 个答案:

没有答案