我在用
<android.support.design.widget.BottomNavigationView/>
Run Code Online (Sandbox Code Playgroud)
在我的Android应用程序中设置BottomNavigationView。我的问题是 如何为Android BottomNavigationView设置通知计数?请帮助我从中脱颖而出。
使用一个Textview创建一个布局,如下所示。
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/notification_badge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|center_horizontal"
android:layout_marginBottom="30dp"
android:layout_marginLeft="20dp"
android:layout_marginStart="20dp"
android:background="@drawable/circlebackground" // Circle background view
android:padding="3dp"
android:textColor="@color/black"
android:textSize="10sp"
tools:text="9+" />
</FrameLayout>
Run Code Online (Sandbox Code Playgroud)
初始化BottomNavgationView之后,您必须为布局充气,然后选择View项目的索引并设置计数。
BottomNavigationView bottomNavigationView = (BottomNavigationView) findViewById(R.id.bottom_navigation);
BottomNavigationMenuView bottomNavigationMenuView =
(BottomNavigationMenuView) bottomNavigationView.getChildAt(0);
View v = bottomNavigationMenuView.getChildAt(1);
BottomNavigationItemView itemView = (BottomNavigationItemView) v;
View badge = LayoutInflater.from(this)
.inflate(R.layout.homescreen_count, bottomNavigationMenuView, false);
TextView tv = badge.findViewById(R.id.notification_badge);
tv.setText("22+");
itemView.addView(badge);
Run Code Online (Sandbox Code Playgroud)
首先,您需要app:actionLayout在菜单文件的项目中添加要显示 BottomNavigationView 徽章的位置,如下所示
<item
android:id="@+id/navigation_chat"
app:actionLayout="@layout/unread_message_layout"
android:icon="@drawable/selector_bottom_navigation_chat"
android:title=""
app:showAsAction="always" />
Run Code Online (Sandbox Code Playgroud)
unread_message_layout带有徽章的布局在哪里,如下所示
这是该布局的 XML
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.appcompat.widget.AppCompatTextView
android:id="@+id/tvUnreadChats"
android:layout_width="@dimen/_15sdp"
android:layout_height="@dimen/_15sdp"
android:layout_gravity="top|center_horizontal"
android:layout_marginLeft="@dimen/_5sdp"
android:background="@drawable/red_circle"
android:gravity="center"
android:textColor="@android:color/white"
android:textSize="@dimen/_7sdp"
android:visibility="visible"
tools:text="5" />
</FrameLayout>
Run Code Online (Sandbox Code Playgroud)
并在您的活动中BottomNavigationView可用的地方添加此内容以显示徽章
var mbottomNavigationMenuView = binding.bottomNavigationView.getChildAt(0) as BottomNavigationMenuView
var chatBadge = LayoutInflater.from(this).inflate(R.layout.unread_message_layout,
mbottomNavigationMenuView, false)
var itemView = mbottomNavigationMenuView.getChildAt(2) as BottomNavigationItemView
val tvUnreadChats = chatBadge.findViewById(R.id.tvUnreadChats) as TextView
tvUnreadChats.text = "1"//String that you want to show in badge
itemView.addView(chatBadge)
Run Code Online (Sandbox Code Playgroud)
这getChildAt(2)是我的第三项BottomNavigationView
| 归档时间: |
|
| 查看次数: |
3050 次 |
| 最近记录: |