【问题标题】:Adding notification badge count on viewpager在 viewpager 上添加通知徽章计数
【发布时间】:2017-05-28 06:19:41
【问题描述】:

我正在尝试在 ViewPager 上的通知选项卡中实现通知徽章计数。我将我的徽章设为TextView,但我不确定我必须将它添加到哪里。我将在下面发布我的代码。

目前,这就是我的通知选项卡的样子。

我遵循了这个例子: 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"/>

【问题讨论】:

    标签: android xml push-notification


    【解决方案1】:

    1) 您可以使用自定义 TabLayout 并为标签提供徽章支持

    这里是Gist

    像这样使用它:

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

    2) 您可以为特定位置创建自定义选项卡视图并使用以下方法设置视图:

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

    详情见Answer

    【讨论】:

      猜你喜欢
      • 2020-05-22
      • 1970-01-01
      • 2020-09-04
      • 2013-09-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-08-07
      • 1970-01-01
      相关资源
      最近更新 更多