2017-05-28 107 views
0

我試圖在ViewPager上的通知標籤上實現通知徽章數。我做了我的徽章TextView,但我不確定我必須添加到哪裏。我會在下面發佈我的代碼。在viewpager上添加通知徽章數

目前,這是我的通知選項卡的外觀。

enter image description here

我接着這個例子: Add new item count to icon on button - Android

tab_item_notification.xml

<?xml version="1.0" encoding="utf-8"?> 
    <selector xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto"> 


     <item 
      android:id="@+id/test" 
      android:state_selected="true" 
      android:drawable="@drawable/ic_notifications_active_white_24dp" /> 


     <item 
      android:id="@+id/test2" 
      android:state_selected="false" 
      android:drawable="@drawable/ic_notifications_active_black_24dp" /> 

    </selector> 

ic_notifications_active_black_24dp.xml

<vector xmlns:android="http://schemas.android.com/apk/res/android" 
      android:width="24dp" 
      android:height="24dp" 
      android:viewportWidth="24.0" 
      android:viewportHeight="24.0"> 
     <path 
      android:fillColor="#BDBDBD" 
      android:pathData="M7.58,4.08L6.15,2.65C3.75,4.48 2.17,7.3 2.03,10.5h2c0.15,-2.65 1.51,-4.97 3.55,-6.42zM19.97,10.5h2c-0.15,-3.2 -1.73,-6.02 -4.12,-7.85l-1.42,1.43c2.02,1.45 3.39,3.77 3.54,6.42zM18,11c0,-3.07 -1.64,-5.64 -4.5,-6.32L13.5,4c0,-0.83 -0.67,-1.5 -1.5,-1.5s-1.5,0.67 -1.5,1.5v0.68C7.63,5.36 6,7.92 6,11v5l-2,2v1h16v-1l-2,-2v-5zM12,22c0.14,0 0.27,-0.01 0.4,-0.04 0.65,-0.14 1.18,-0.58 1.44,-1.18 0.1,-0.24 0.15,-0.5 0.15,-0.78h-4c0.01,1.1 0.9,2 2.01,2z"/> 


    </vector> 

badge_circle.xml

<shape xmlns:android="http://schemas.android.com/apk/res/android" 
    android:shape="oval"> 
    <solid 
     android:color="#F00" /> 
    <stroke 
     android:width="2dip" 
     android:color="#FFF" /> 
    <padding 
     android:left="5dip" 
     android:right="5dip" 
     android:top="5dip" 
     android:bottom="5dip" /> 
</shape> 

我想實現這一點,但我不知道我添加此到哪裏。有人在哪裏添加?

<TextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="10" 
    android:textColor="#FFF" 
    android:textSize="16sp" 
    android:textStyle="bold" 
    android:background="@drawable/badge_circle"/> 

回答

0

1)可以使用AA型定製TabLayout與標籤

這裏徽章的支持是Gist

這樣使用它:

yourBadgetTabLayoutObject.with(desireTabPosition).badge(true).badgeCount(1).build(); 

2)您可以創建自定義tabview爲特定位置並設置視圖使用:

TabLayout.Tab tab = tabLayout.getTabAt(i); 
tab.setCustomView(pagerAdapter.getTabView(i)); 

詳情請參閱Answer