Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
441 views
in Technique[技术] by (71.8m points)

java - Tabhost- Android ViewBadger badge issue

In my application i want to show the badges on one tab. For that i used the android-viewbadger.jar file Android ViewBadger i am setting my badge on tab-2 means Notification tab and i am setting my badge like this

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

When i set my badge it will show on right side like this AVD sampleImage

I have also tried to change the tabs number

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

but it always show on right side Here is code i am using

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

checking to enable Layout Bound option setting=>Developer Option => Layout Bound enter image description here

when i use public View getChildTabViewAt (int index); method from TabWidget Documentation

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

 my output will be this 

enter image description here

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...