代码之家  ›  专栏  ›  技术社区  ›  Ravindra Kushwaha

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

  •  10
  • Ravindra Kushwaha  · 技术社区  · 7 年前

    我正在使用 Bottomnavigationview 对于我的应用程序中的选项卡栏,我使用以下代码。请检查一下。


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

    我得到以下信息

    Current Result

    但问题是 ,我需要 ,如下图所示 红色 TextView 计算位于 ,请查看下图。

    Expected Result

    我已经搜索过了,得到了 第三方库 Click here . 是否不可能不使用任何第三方库而只使用 为了它?

    我在这里搜索过 但是没有得到预期的结果,请检查下面我访问过的链接。

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

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

    3 回复  |  直到 5 年前
        1
  •  11
  •   Ravindra Kushwaha    5 年前

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

    布局

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

    然后在你的徽章上画下

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

    最后,你需要我们把徽章的布局扩大到 MainActivity.java

        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);
    
        2
  •  3
  •   Hkh    5 年前

      BottomNavigationView mbottomNavigationView =findViewById(R.id.navigation);
    
            BottomNavigationMenuView mbottomNavigationMenuView =
                    (BottomNavigationMenuView) mbottomNavigationView.getChildAt(0);
    
            View view = mbottomNavigationMenuView.getChildAt(1);
    
            BottomNavigationItemView itemView = (BottomNavigationItemView) view;
    
            View cart_badge = LayoutInflater.from(this)
                    .inflate(R.layout.notification_badge,
                            mbottomNavigationMenuView, false);
    
            itemView.addView(cart_badge);
    
        3
  •  1
  •   Morteza Rastgoo    6 年前

    我使用了带有自定义视图的选项卡布局

        <com.google.android.material.tabs.TabLayout
            android:id="@+id/mainTabBar"
            android:layout_width="match_parent"
            android:layout_height="48dp"
            android:layout_alignParentBottom="true"
            android:background="@color/colorPrimaryDark"
            app:tabGravity="fill"
            app:tabIndicatorHeight="0dp"
            app:tabMode="fixed"
            app:tabSelectedTextColor="@color/colorPrimary" />
    
        var mainPagerAdapter = MainPagerAdapter(supportFragmentManager!!)
            viewPager.setAdapter(mainPagerAdapter)
            viewPager.setOffscreenPageLimit(mainPagerAdapter.getCount())
            for (i in 0 until mainPagerAdapter.getCount()) {
                val tab = mainTabBar.newTab()
                val tabView = mainPagerAdapter.getTabView(i, this)
                tab.setCustomView(tabView)
                mainTabBar.addTab(tab)
            }
            val tab = mainTabBar.getTabAt(0)
            if (tab != null) {
                tab.select()
                val view = tab.customView
                mainPagerAdapter.changeColorOfTabView(view!!, true, this,tab.position)
            }
            viewPager.setCurrentItem(0)
    
            mainTabBar.addOnTabSelectedListener(object : TabLayout.OnTabSelectedListener {
                override fun onTabSelected(tab: TabLayout.Tab) {
                    viewPager.currentItem = tab.getPosition()
                    val view = tab.getCustomView()
                    mainPagerAdapter.changeColorOfTabView(view!!, true, this@MainActivity, tab.position)
                }
    
                override fun onTabUnselected(tab: TabLayout.Tab) {
                    val view = tab.getCustomView()
                    mainPagerAdapter.changeColorOfTabView(view!!, false, this@MainActivity, tab.position)
                }
    
                override fun onTabReselected(tab: TabLayout.Tab) {}
            })
    
    
        fun getTabView(i: Int, context: Context): View {
    //        val typeface = Typeface.createFromAsset(context.getAssets(), "fonts/Vazir-FD.ttf")
            val tabView = LayoutInflater.from(context).inflate(R.layout.tab_main, null) as LinearLayout
    
            val text = tabView.findViewById<TextView>(R.id.tabTextView)
            val icon = tabView.findViewById<ImageView>(R.id.drawableTop)
            text.setText(getPageTitle(i))
            text.setTextColor(context.getResources().getColor(R.color.white))
    //        text.setTypeface(typeface)
            icon.setImageResource(getIconResId(i))
    
            if (i == 3) {
                tabView.isSelected = true
            }
            return tabView
        }
    

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:paddingTop="10dp"
        android:orientation="vertical">
    
        <ImageView
            android:id="@+id/drawableTop"
            android:layout_width="30dp"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_centerHorizontal="true"
            android:adjustViewBounds="true" />
    
        <TextView
            android:id="@+id/tabTextView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/drawableTop"
            android:layout_centerHorizontal="true"
            android:textColor="@color/toggle_text_color_gray_red"
            android:textSize="11sp" />
    </LinearLayout>