【问题标题】:How to create the custom item of Bottomnavigationview Android?如何创建Bottomnavigationview Android的自定义项?
【发布时间】:2017-08-03 13:00:49
【问题描述】:

我在我的应用程序中使用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" />

我从中得到以下结果,请查看图片。

但问题是,我需要Bottomnavigationview 的自定义项,如下图有红色 @987654334 @count 在Bottomnavigationview 的主页上,请查看下图。

我已经搜索了它并获得了它的第三方库,它能够创建我想要的视图。请检查它的库@987654323 @ .难道不能不使用任何第三方库而只使用Bottomnavigationview吗?

我在 SO 上搜索过这里,但没有得到预期的结果,请查看我访问过的以下链接。

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

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

【问题讨论】:

  • 无需使用bottomsheet,创建自己的自定义图像和文本视图
  • @MohitSuthar ..我想你在说It is not a good practice 。我有标签的功能,所以我正在使用标签。创建视图并在每个屏幕上扩展该视图更多繁琐的工作兄弟..任何方式感谢您的意见..请建议我另一种方式
  • @KuLdipPaTel 请检查你曾经分享过的链接..它与这个问题无关
  • @MortezaRastgoo 我已经完成了上述任务.. 明天肯定会更新你,因为我大约 2 年前问过它
  • @MortezaRastgoo,请查看下方更新的解决方案,如有疑问请告知

标签: java android android-custom-view bottomnavigationview


【解决方案1】:

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

布局

 <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 中,我们在其中使用 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);

【讨论】:

  • 我们可以使用相同的方法在 ActionBar 菜单中设置自定义文本吗?
  • 意思是,需要在右侧或左侧的工具栏上添加圆形视图的计数器类型?
  • 是的,我们可以为这些工具栏设置自定义文本吗?
【解决方案2】:

如果您使用的是androidX,请参考以下代码:

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

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

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

    tab_main.xml

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

    【讨论】:

    • 是的,它是可定制的,您可以使用自定义 UI 做任何您想做的事情
    • 当然,当您想要自定义所有标签时,它很方便
    • 这个解决方案是否支持平板电脑实际上我正面临this issue
    • 这个支持平板但不占满空间
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-13
    • 1970-01-01
    • 1970-01-01
    • 2020-01-29
    • 2012-02-17
    相关资源
    最近更新 更多