简体   繁体   English

使用BottomNavigationView小部件android在底部导航栏中不显示菜单

[英]menu is not visible in Bottom Navigation bar using BottomNavigationView widget android

I am trying to add 5 icons on the bottom of the navigation bar. 我正在尝试在导航栏底部添加5个图标。 and this is what it came out like pic1 . 这就是像pic1这样出来的。 When I click on the bottom, other icons would show up only one at a time, shown in pic2 . 当我点击下方,其它图标将出现一次只能有一个时间,在显示PIC2 I need to make it look like the this picture, pic3 . 我需要使其看起来像这张图片pic3 Does anyone have any idea on what's wrong with the code? 有人对代码出什么问题有任何想法吗?

These are the libraries that I am using: 这些是我正在使用的库:

   implementation 'com.android.support:support-core-utils:27.1.1'
implementation 'com.android.support:appcompat-v7:27.1.1'
implementation 'com.android.support:design:27.1.1'

Here is my bottom navigation layout view: 这是我的底部导航布局视图:

<merge xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:layout_alignParentBottom="true">

    <android.support.design.widget.BottomNavigationView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/bottomNavViewBar"
        android:background="@drawable/white_grey_border_top"
        app:menu="@menu/bottom_navigation_menu">

    </android.support.design.widget.BottomNavigationView>

</RelativeLayout>

Here is white_grey_border_top drawable file: 这是white_grey_border_top可绘制文件:

 <?xml version="1.0" encoding="utf-8"?>
 <layer-list xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="1dp">

<item
    android:bottom="-1dp"
    android:top="1dp"
    android:right="-1dp"
    android:left="-1dp">

    <shape
        android:shape="rectangle">
        <stroke
            android:width="1dp"
            android:color="@color/grey" />
        <solid
            android:color="@color/white" />

    </shape>

</item>

Here is my bottom_navigation_menu: 这是我的bottom_navigation_menu:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">


<item
    android:id="@+id/ic_house"
    android:icon="@drawable/ic_house"
    android:title="">

</item>

<item
    android:id="@+id/ic_search"
    android:icon="@drawable/ic_search"
    android:title="">

</item>

<item
    android:id="@+id/ic_circle"
    android:icon="@drawable/ic_circle"
    android:title="">

</item>

<item
    android:id="@+id/ic_alert"
    android:icon="@drawable/ic_alert"
    android:title="">

</item>

<item
    android:id="@+id/ic_android"
    android:icon="@drawable/ic_android"
    android:title="">

</item>

update: when I add android:backgroundTint="#1091BF" to my bottom navigation layout view, then it works but the background is blue ( pic4 ). 更新:当我将android:backgroundTint="#1091BF"到我的底部导航布局视图中时,它可以工作,但背景为蓝色( pic4 )。 Also, I notice that if I just change the android:color="@color/white" in my white_grey_border_top drawable file to any other color then all the icon would show up. 另外,我注意到,如果仅将white_grey_border_top可绘制文件中的android:color="@color/white"更改为任何其他颜色,则所有图标都会显示。

Try using the following inside your bottom navigation layout view. 尝试在底部导航布局视图中使用以下内容。

<?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"
android:background="@drawable/background"
>
<FrameLayout
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    />

<android.support.design.widget.BottomNavigationView
    android:id="@+id/navigationView"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_marginEnd="0dp"
    android:layout_marginStart="0dp"
    android:background="?android:attr/windowBackground"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    android:backgroundTint="#1091BF"
    app:itemIconTint="#fff"
    app:itemTextColor="#fff"
    app:menu="@menu/navigation"/>

    </android.support.constraint.ConstraintLayout>

Make sure to add dependency 确保添加依赖项

implementation 'com.android.support.constraint:constraint-layout:1.1.2'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM