Bottom navigation view with custom item (actionLayout)

前端 未结 1 1488
失恋的感觉
失恋的感觉 2021-02-13 14:45

I would like to add a custom item in the new BottomNavigationView .

There are plenty of tutorial of adding a custom view with the normal navigation view but I can\'t fin

相关标签:
1条回答
  • 2021-02-13 15:21

    Hope the below solution will help.

    1. Create a layout : layout/_custom_cart_item_layout.xml

      <?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/cart_badge"
      android:layout_width="20dp"
      android:layout_height="20dp"
      android:layout_gravity="top|center_horizontal"
      android:layout_marginLeft="10dp"
      android:background="@drawable/circle"
      android:backgroundTint="@color/colorAccent"
      android:gravity="center"
      android:text="0"
      android:textColor="@android:color/white"
      android:textSize="12sp" />
      
      </FrameLayout>
      
    2. drawable/circle

      <?xml version="1.0" encoding="utf-8"?>
      <shape xmlns:android="http://schemas.android.com/apk/res/android" 
      android:shape="oval">
      <solid android:color="@color/white"/>
      <size android:width="40dp" android:height="40dp" />
      </shape>
      
    3. MainActivity : layout/main_activity.xml

      <?xml version="1.0" encoding="utf-8"?>
      <android.support.constraint.ConstraintLayout
      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"
      tools:context="com.MainActivity">
      
      <android.support.design.widget.BottomNavigationView
      android:id="@+id/bot_nav"
      android:layout_width="0dp"
      android:layout_height="56dp"
      android:background="@color/white"
      android:elevation="2dp"
      app:layout_constraintBottom_toBottomOf="parent"
      app:layout_constraintEnd_toEndOf="parent"
      app:layout_constraintStart_toStartOf="parent"
      app:menu="@menu/bottom_menu" />
      
      </android.support.constraint.ConstraintLayout>
      
    4. menu : menu/bottom_menu.xml

      <?xml version="1.0" encoding="utf-8"?>
      <menu xmlns:app="http://schemas.android.com/apk/res-auto"
      xmlns:android="http://schemas.android.com/apk/res/android">
      
      <item
      android:id="@+id/menu_home"
      android:icon="@drawable/home24"
      android:title="Home" />
      
      <item
      android:id="@+id/menu_cart"
      android:icon="@drawable/shoppingcart24"
      android:title="Cart" />
      <item
      android:id="@+id/menu_acct"
      android:icon="@drawable/user24"
      android:title="Account" />
      </menu>
      
    5. In your MainActivity class within onCreate

      BottomNavigationView mbottomNavigationView =findViewById(R.id.bot_nav);
      
      BottomNavigationMenuView mbottomNavigationMenuView =
      (BottomNavigationMenuView) mbottomNavigationView.getChildAt(0);
      
      View view = mbottomNavigationMenuView.getChildAt(1);
      
      BottomNavigationItemView itemView = (BottomNavigationItemView) view;
      
      View cart_badge = LayoutInflater.from(this)
      .inflate(R.layout._custom_cart_item_layout,
       mbottomNavigationMenuView, false);
      
      itemView.addView(cart_badge);
      

    Output : Image

    Hope it will work similar for you.

    Thank you

    0 讨论(0)
提交回复
热议问题