Adding badge to tab in android

后端 未结 5 1690
生来不讨喜
生来不讨喜 2021-02-04 21:38

i want to add badge to tab in my app as in iPhone.

Screen shot of badge used in iPhone is in the following link:

5条回答
  •  一整个雨季
    2021-02-04 22:40

    This is an example of How to add a badge in tab

    chat_tab.xml

    
    
        
    
        
    
        
    
    
    
    

    This is badge.xml (red circle for notifications background),TextView id:new_notifications background

    
    
    
        
    
        
    
        
    
        
    
    
    

    Then in the code you can simply do

    LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    
            View chatTab = inflater.inflate(R.layout.chat_tab, null);
    
            tvNewNotifications = (TextView) chatTab.findViewById(R.id.new_notifications);
    
            intent = new Intent().setClass(MainTab.this, Chat.class);
            tabSpec = tabHost
                    .newTabSpec("chat")
                    .setIndicator(chatTab)
                    .setContent(intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP));
    

    As you can see my Relative Layout has a background @drawable/tab_indicator the tab indicator.xml is the framework's standard drawable of the tab,which i got from the sdk,i suggest you should also get it from the folder of the api in sdk as you also need to copy some images from the drawable folders,you can find it your_sdk_drive:\sdk\platforms\android-8

提交回复
热议问题