gpt4 book ai didi

Android BottomNavigationView 带徽章

转载 作者:行者123 更新时间:2023-12-02 16:56:17 27 4
gpt4 key购买 nike

Android 中不支持向 BottomNavigationView 添加徽章。

如何将带有数字的徽章添加到 BottomNavigationView 中的特定选项卡。我需要它在没有第三方库的情况下本地完成。

我正在使用 Xamarin native 和 MvvmCross。

最佳答案

---注意---

徽章很快就会得到开箱即用的支持。但是,如果您确实想向选项卡添加自定义 View ,这可能会很有用。

使用 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);
}

绑定(bind)徽章文本

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

结果

enter image description here

关于Android BottomNavigationView 带徽章,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52439196/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com