我试图在BottomNavigationView没有使用任何库的情况下为项目添加徽章,但不知何故BottomNavigationView没有显示徽章(custom_view)
main_view.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.hrskrs.test.MainActivity">
<FrameLayout
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<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:itemBackground="@color/colorPrimary"
app:itemIconTint="@color/colorAccent"
app:itemTextColor="@color/colorPrimaryDark"
app:menu="@menu/bottom_navigation_main" />
</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)
bottom_navigation_menu.xml:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/item_test"
android:icon="@mipmap/ic_launcher"
android:title="action1"
app:showAsAction="always" />
<item
android:enabled="true"
android:icon="@mipmap/ic_launcher"
android:title="action2"
app:showAsAction="ifRoom" />
<item
android:enabled="true"
android:icon="@mipmap/ic_launcher"
android:title="action3"
app:showAsAction="ifRoom" />
</menu>
Run Code Online (Sandbox Code Playgroud)
活动扩展自AppCompatActivity:
@Override
public boolean onCreateOptionsMenu(Menu menu) {
menu = bottomNavigationView.getMenu();
menu.clear();
getMenuInflater().inflate(R.menu.bottom_navigation_main, menu);
MenuItem item = menu.findItem(R.id.item_test);
item = MenuItemCompat.setActionView(item, R.layout.custom_view); …Run Code Online (Sandbox Code Playgroud) android android-layout bottomnavigationview material-components material-components-android
以下是我的依赖
implementation 'com.google.android.material:material:1.0.0'
implementation 'androidx.appcompat:appcompat:1.0.2'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
Run Code Online (Sandbox Code Playgroud)
我的layout.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/main_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffffff"
android:fitsSystemWindows="true">
<com.google.android.material.bottomappbar.BottomAppBar
android:id="@+id/bottomBar"
style="@style/Widget.MaterialComponents.BottomAppBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:backgroundTint="@color/colorPrimary"
app:fabAlignmentMode="center"
app:fabCradleMargin="10dp"
app:fabCradleVerticalOffset="4dp"
app:navigationIcon="@drawable/ic_drawer" >
</com.google.android.material.bottomappbar.BottomAppBar>
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_anchor="@id/bottomBar"
app:srcCompat="@drawable/ic_apps" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>
Run Code Online (Sandbox Code Playgroud)
上面的代码对我来说很好
但是,当我更新dependencies的material design
implementation 'com.google.android.material:material:1.1.0-alpha01'
Run Code Online (Sandbox Code Playgroud)
我遇到以下错误:
java.lang.RuntimeException: Unable to start activity ComponentInfo{neel.com.bottomappbar/neel.com.bottomappbar.MainActivity}: android.view.InflateException: Binary XML file line #11: Binary XML file line #11: Error inflating class com.google.android.material.bottomappbar.BottomAppBar
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2485)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2545)
at android.app.ActivityThread.access$1100(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1396)
at android.os.Handler.dispatchMessage(Handler.java:102) …Run Code Online (Sandbox Code Playgroud) 我已经BottomNavigationView根据这个例子实现了徽章计数
/sf/answers/3943845041/
它工作正常,但我想更改徽章的背景颜色有什么办法吗?
android material-design bottomnavigationview material-components material-components-android
<item
android:id="@+id/nav_gallery"
app:actionViewClass="android.widget.TextView"/>
Run Code Online (Sandbox Code Playgroud)
这是底部导航的菜单。
TextView gallery=(TextView)
MenuItemCompat.getActionView(navigationView.getMenu().
findItem(R.id.nav_gallery)); //getting menu item of bottom nav view
gallery.setText("99+");
Run Code Online (Sandbox Code Playgroud)
但是此代码不适用于底部导航视图。BottomNavigationView 显示时没有设置任何通知计数器。
我只想补充徽章了menuItem的BottomNavigationView我的应用程序。我使用的BottomNavigationView是材料组件库(版本1.1.0-alpha08),因为它的最新版本是在7天前发布的,所以我没有找到与此相同的任何教程,因为该版本的BottomNavigationView“的showBadge方法,我们不能使用该方法。
我试过了的实例调用getBadge和getOrCreateBadge方法BottomNavigationView。
BottomNavigationView bottomNavigationView = findViewById(R.id.bottom_nav);
if (bottomNavigationView.getBadge(3) == null) {
audioPlayingCountBadge = bottomNavigationView.getOrCreateBadge(3);
audioPlayingCountBadge.setBackgroundColor(getResources().getColor(R.color.colorPrimaryDark));
} else {
audioPlayingCountBadge = bottomNavigationView.getBadge(3);
}
audioPlayingCountBadge.setVisible(true);
Run Code Online (Sandbox Code Playgroud)
我在android开发中是菜鸟,所以如果有人可以提供详细解决此问题的方法,这将非常感激我。
不支持在Android中的BottomNavigationView中添加徽章.
如何将带有数字的徽章添加到BottomNavigationView中的特定选项卡.我需要它本地完成没有第三方库.
我正在使用Xamarin原生的MvvmCross.