如何创建Bottomnavigationview Android的自定义项?

Rav*_*aha 6 java android android-custom-view bottomnavigationview

Bottomnavigationview在我的应用程序中使用for标签栏,为此,我正在使用以下代码。请检查一下。

布局:-

<android.support.design.widget.BottomNavigationView
        android:id="@+id/bottom_navigation"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"     
        app:itemTextColor="@android:color/black"
        app:menu="@menu/bottom_navigation_main" />
Run Code Online (Sandbox Code Playgroud)

我从中得到以下结果,请检查它的图像。

当前结果

但是问题是,我需要的自定义项目Bottomnavigationview,如下面的图像中所示,该项目的首页上有红色 TextView计数Bottomnavigationview,请检查以下图像。

预期结果

我已经搜索了它,并获得了它的第三方库,它能够创建我想要的视图。请检查它的库。单击此处不能不使用任何第三方库而仅Bottomnavigationview使用它吗?

我在此处搜索过SO,但未获得预期结果,请检查我访问过的以下链接。

1.第一链接
2.第二链接
3.第三链接
4.第四链接
5.第五链接

请帮助我解决这个问题。谢谢 :)

Rav*_*aha 9

我已经按照以下方法完成了上述任务,请查看解决方案

布局

 <android.support.design.widget.BottomNavigationView
            android:id="@+id/bottom_navigation"
            android:layout_width="match_parent"
            android:layout_height="60dp"
            android:layout_alignParentBottom="true"
            android:background="@color/header_color"
            app:itemIconTint="@color/white_color"
            app:itemTextColor="@color/white_color"
            app:menu="@menu/bottom_navigation" />  /// YOUR MENU ITEMS FOR THE BOTTOM NAVIGATION
Run Code Online (Sandbox Code Playgroud)

您的徽章计数布局如下

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/notificationsBadge"
        android:layout_width="25dp"
        android:layout_height="25dp"
        android:layout_gravity="top|center_horizontal"
        android:layout_marginLeft="10dp"
        android:layout_marginStart="10dp"
        android:background="@drawable/circle_red"
        android:gravity="center"
        android:padding="4dp"
        android:text=""
        android:textColor="@android:color/white"
        android:textSize="12sp" />
</FrameLayout>
Run Code Online (Sandbox Code Playgroud)

然后你的徽章可绘制如下

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="oval">
    <solid android:color="@android:color/holo_red_light" />
    <size
        android:width="20dp"
        android:height="20dp" />
</shape>
Run Code Online (Sandbox Code Playgroud)

最后,您需要将我们的徽章布局膨胀到我们的MainActivity.java 中,我们在其中使用 BottomNavigationView,请查看以下代码。

    BottomNavigationMenuView mbottomNavigationMenuView =
            (BottomNavigationMenuView) mBinding.bottomNavigation.getChildAt(0);

    View view = mbottomNavigationMenuView.getChildAt(4);

    BottomNavigationItemView itemView = (BottomNavigationItemView) view;

    View cart_badge = LayoutInflater.from(this)
            .inflate(R.layout.profile_view,
                    mbottomNavigationMenuView, false);

    //// AND THAN SET THE COUNTER BADGE, AS FOLLOW
    ((TextView) cart_badge.findViewById(R.id.notificationsBadge)).setText("5");

    itemView.addView(cart_badge);
Run Code Online (Sandbox Code Playgroud)