带有标记的Android底部导航视图

6

在Android的BottomNavigationView中,不支持添加徽章(badge)。

我该如何在BottomNavigationView的特定标签上添加带数字的徽章(badge)?我需要使用本地方法完成,不能使用第三方库。

我正在使用MvvmCross的Xamarin原生框架。


哦,好吧。我遇到了一个问题,找到了答案,想让其他人知道,特别是在阅读了“Stack Exchange一直明确鼓励用户回答自己的问题”之后。不确定这个问题为什么会被认为是“过于宽泛”,尤其是当这个问题以前已经被问过了,但我需要不使用第三方库的解决方案。 - c.lamont.dev
2个回答

11

---注意---

徽章将很快被支持。但是,如果您确实想要将自定义视图添加到选项卡中,这可能会很有用。

使用BottomNavigationView创建布局

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:local="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <FrameLayout
        android:id="@+id/tabsRootFrameLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@+id/tabsRootBottomNavigation" />
    <android.support.design.widget.BottomNavigationView
        android:id="@+id/tabsRootBottomNavigation"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:background="@android:color/white"
        local:menu="@menu/root_bottom_navigation_menu"
        local:itemIconTint="@color/bottom_navigation_selector"
        local:itemTextColor="@color/bottom_navigation_selector"
        local:elevation="16dp" />
</RelativeLayout>

菜单:root_bottom_navigation_menu.xml

<?xml version="1.0" encoding="utf-8"?>
<menu
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">
    <item
        android:id="@+id/tab_search"
        android:enabled="true"
        android:icon="@drawable/search_icon"
        app:showAsAction="ifRoom" />
    <item
        android:id="@+id/tab_profile"
        android:enabled="true"
        android:icon="@drawable/profile_icon"
        app:showAsAction="ifRoom" />
    <item
        android:id="@+id/tab_my_car"
        android:enabled="true"
        android:icon="@drawable/car_icon"
        app:showAsAction="ifRoom" />
    <item
        android:id="@+id/tab_notifications"
        android:enabled="true"
        android:icon="@drawable/bell_icon"
        app:showAsAction="ifRoom" />
</menu>

创建徽章布局:component_tabbar_badge.axml

<?xml version="1.0" encoding="UTF-8"?>
<FrameLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="@dimen/margin_tiny">
    <TextView
        android:id="@+id/notificationsBadgeTextView"
        android:layout_width="16dp"
        android:layout_height="16dp"
        android:layout_gravity="top|center_horizontal"
        android:layout_marginLeft="10dp"
        android:layout_marginStart="10dp"
        android:background="@drawable/notification_red_dot"
        android:gravity="center"
        android:textColor="@color/white"
        android:textSize="9dp" />
</FrameLayout>

红点背景:notification_red_dot.xml

<?xml version="1.0" encoding="UTF-8"?>
<shape
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="oval">
    <solid
        android:color="@color/red" />
    <size
        android:width="10dp"
        android:height="10dp" />
</shape>

将徽章布局添加到底部导航

private void SetNotificationBadge()
{
    _bottomNavigation = FindViewById<BottomNavigationView>(Resource.Id.tabsRootBottomNavigation);
    var notificationsTab = _bottomNavigation.FindViewById<BottomNavigationItemView>(Resource.Id.tab_notifications);
    View badge = LayoutInflater.From(this).Inflate(Resource.Layout.component_tabbar_badge, notificationsTab, false);
    _notificationBadgeTextView = badge.FindViewById<TextView>(Resource.Id.notificationsBadgeTextView);
    notificationsTab.AddView(badge);
}

绑定徽章文本

    var set = this.CreateBindingSet<TabsRootActivity, TabsRootViewModel>();
    set.Bind(_notificationBadgeTextView).To(vm => vm.UnreadNotificationsCount);
    set.Apply();

结果

输入图片描述


如何删除 if 0 - Ravi Vaghela
_notificationBadgeView.Visibility = ViewStates.Gone - c.lamont.dev

0

这将对您有所帮助

参考:在底部导航栏的图标上显示徽章

当使用支持库的底部导航栏时,在菜单项上显示徽章/通知会比较复杂。然而,有简单的解决方案可以完成此操作,例如https://github.com/aurelhubert/ahbottomnavigation

这个库是底部导航栏的更高级版本。您可以使用以下代码片段简单地设置菜单项上的徽章。

bottomNavigation.setNotification(notification, bottomNavigation.getItemsCount() - 1);

如果您不想使用第三方库,那么没问题。
查看更多信息,请点击 在底部导航栏图标上显示徽章

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接