如何创建Bottomnavigationview Android的自定义项? [英] How to create the custom item of Bottomnavigationview Android?

查看:626
本文介绍了如何创建Bottomnavigationview Android的自定义项?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am using the Bottomnavigationview for tab-bar in my application, for it, i am using the following code.Please check it once.

布局:-

<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" />

我从中得到以下结果,请检查该图像.

And I get the following Result from it, please check the image for it.

但是问题是 ,我需要Bottomnavigationview的自定义项,就像下面的图片中有 red 计数在Bottomnavigationview的首页上,请检查下图.

But the problem is that, I need the custom item of the Bottomnavigationview, like in below image there is red TextView count which is on the home item of the Bottomnavigationview, Please check the below image.

我已经搜索了它,并获得了 第三方库 ,它可以创建我想要的视图.请检查它的库单击此处. 是否不能不使用任何第三方库而仅使用<是c0>吗?

I have searched for it and get the third party library for it, which is able to create the view which I want.Please check the library of it Click here .Is it not possible to NOT use any third party library and use the only Bottomnavigationview for it?

我在 SO 上进行了搜索,但未获得预期的结果,请检查以下我访问过的链接.

I have searched here on SO but did not get the expected result, please check the below link which I have visited.

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

1. First Link
2. Second Link
3. Third Link
4. Forth Link
5. Fifth Link

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

Please help me to short out from this problem. Thanks :)

推荐答案

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

I have done the above task as the following approach, please have a look on the solution

布局

 <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

您的徽章计数布局如下

<?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>

与您的徽章可绘制之后相比,如下所示

And than after your badge drawable be like below

<?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>

最后,您需要将徽章布局添加到我们使用BottomNavigationView的 MainActivity.java 中,请检查以下代码.

And at the last, you need we need to inflate our badge layout into our MainActivity.java , where we use the BottomNavigationView, please check the following code for it.

    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);

这篇关于如何创建Bottomnavigationview Android的自定义项?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆