如何在BottomNavigationMenu上制作选中按钮?

问题描述 投票:0回答:1

我需要找到一种方法来检查任何其他BottomNavigationMenu按钮,但不是第一次打开一个活动时。

我已经阅读了大量的文档并在谷歌搜索,并看到达到我的目标我应该在我的菜单项文件中使用android:checked =“true”。但它在我的情况下不起作用。

这是我的bottom_navigation.xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:id="@+id/appointments_button"
        android:title="@string/appointment_menu_link"
        android:icon="@drawable/calendar_white"
        android:checked="false"/>
    <item android:id="@+id/booking_button"
        android:title="@string/booking_menu_link"
        android:icon="@drawable/plus_white"
        android:checked="true"/>
    <item android:id="@+id/user_account"
        android:title="@string/profile_menu_link"
        android:icon="@drawable/user_account"
        android:checked="false"/>
</menu>

在我的活动文件employee_list.xml下面

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
    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/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:fitsSystemWindows="true">

    <!-- Layout to contain contents of main body of screen (drawer will slide over this) -->
    <android.support.constraint.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".BarbersActivity">

        <RelativeLayout
            android:layout_height="match_parent"
            android:layout_width="match_parent"
            android:paddingTop="?android:attr/actionBarSize">
            <ProgressBar
                android:id="@+id/progressBar"
                android:visibility="visible"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                style="@style/AppTheme"
                android:layout_centerInParent="true"/>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical"
                android:layout_marginBottom="56dp">

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="64dp"
                    android:fontFamily="roboto-medium"
                    android:gravity="center_vertical"
                    android:paddingLeft="16dp"
                    android:text="@string/activity_masters"
                    android:textColor="#D9000000"
                    android:textFontWeight="500"
                    android:textSize="20sp"
                    android:textStyle="normal" />

                <ListView
                    android:id="@+id/list"
                    android:orientation="vertical"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:drawSelectorOnTop="true"/>

            </LinearLayout>

            <android.support.design.widget.BottomNavigationView
                xmlns:android="http://schemas.android.com/apk/res/android"
                xmlns:app="http://schemas.android.com/apk/res-auto"
                android:id="@+id/bottom_navigation"
                android:layout_width="match_parent"
                android:layout_height="56dp"
                android:layout_gravity="bottom"
                android:background="@color/primary_dark_color"
                app:menu="@menu/bottom_navigation"
                android:layout_alignParentBottom="true"
                app:itemIconTint="@drawable/bottom_navigation_black_icon_color"
                app:itemTextColor="@drawable/bottom_navigation_black_text_color">
            </android.support.design.widget.BottomNavigationView>
        </RelativeLayout>

        <include
            android:id="@+id/toolbar"
            layout="@layout/toolbar"/>

    </android.support.constraint.ConstraintLayout>

    <!-- Container for contents of drawer - use NavigationView to make configuration easier -->
    <android.support.design.widget.NavigationView
    android:id="@+id/nav_view"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:fitsSystemWindows="true"
    app:menu="@menu/drawer_view"
    app:headerLayout="@layout/nav_header"
    android:background="#FFFFFF" />
</android.support.v4.widget.DrawerLayout>

我还在我的build.gradle依赖项中添加了api'com.android.support:design:28.0.0'。

请告知是否有可能实现我的目标。

android
1个回答
0
投票

我认为这只能通过代码完成。试试这个:在调用bottomNavigationView.setOnNavigationItemSelectedListener(...)之后,添加以下代码行:

bottomNavigationView.setSelectedItemId(R.id.booking_button);

或者:bottomNavigationView.getMenu().findItem(R.id.booking_button).setChecked(true);

这应该在BottomNavigationBar中选择所需的菜单项。

© www.soinside.com 2019 - 2024. All rights reserved.