菜单项中的单个可检查行为不起作用

时间:2019-06-22 12:18:44

标签: android android-xml android-menu

我在BottomSheetDialog中绑定了一个菜单,如下所示:

bottom sheet dialog

我希望在选择项目时对其进行检查。它应具有单个可检查的行为,例如:

Desired Bottom Sheet Dialog

请在下面找到代码:

menu_bottomappbar_drawer.xml

<menu xmlns:android="http://schemas.android.com/apk/res/android">
<group android:checkableBehavior="single">
    <item
            android:id="@id/BottomAppBar_fromActivityHome_DrawerMenu_Home"
            android:icon="@drawable/ic_home_dark"
            android:title="@string/Menu_BottomAppBar_DrawerMain_Home"/>
    <item
            android:id="@id/BottomAppBar_fromActivityHome_DrawerMenu_Goals"
            android:icon="@drawable/ic_goals_dark"
            android:title="@string/Menu_BottomAppBar_DrawerMain_Goals"/>

    <item
            android:id="@id/BottomAppBar_fromActivityHome_DrawerMenu_ToDo"
            android:icon="@drawable/ic_todo_dark"
            android:title="@string/Menu_BottomAppBar_DrawerMain_ToDo"/>

    <item
            android:id="@id/BottomAppBar_fromActivityHome_DrawerMenu_ToBuy"
            android:icon="@drawable/ic_tobuy_dark"
            android:title="@string/Menu_BottomAppBar_DrawerMain_ToBuy"/>

    <item
            android:id="@id/BottomAppBar_fromActivityHome_DrawerMenu_Statistics"
            android:icon="@drawable/ic_statistics_dark"
            android:title="@string/Menu_BottomAppBar_DrawerMain_Statistics"/>
</group>
</menu>

菜单绑定到的navigationView:

<com.google.android.material.navigation.NavigationView
            android:id="@id/BottomAppBar_fromFragmentBottomAppBarDrawer_Drawer_NavigationView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom"
            android:layout_marginTop="@dimen/MarginTop_forNavigationView"
            android:paddingBottom="@dimen/PaddingTop_forNavigationView"
            app:menu="@menu/menu_bottomappbar_drawer"
            app:itemBackground="@drawable/custom_bottomappbar_drawer_item_state"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/BottomAppBar_fromFragmentBottomAppBarDrawer_Drawer_UserIcon"
            app:layout_constraintBottom_toBottomOf="parent"/>

custom_bottomappbar_drawer_item_state.xml

<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@color/colorAccent" android:state_checked="true" />
<item android:drawable="@color/white_000" />
</selector>

编辑:

我注意到,如果我使用导航组件这样打开底部工作表对话框:

            this.findNavController(R.id.Container_fromHomeActivity_BottomAppBarFragments).navigate(R.id.BottomAppBar_fromActivityHome_Drawer)

检查行为消失。

但是,如果我这样手动打开对话框:

           val mBottomNavDrawerFragment = RoundedBottomSheetDialogFragmentForBottomAppBarDrawer()
            mBottomNavDrawerFragment.show(supportFragmentManager, mBottomNavDrawerFragment.tag)

检查行为还可以。为什么会这样呢?

0 个答案:

没有答案