Android移除导航菜单项突出显示

sbtkgmzw  于 2023-05-27  发布在  Android
关注(0)|答案(4)|浏览(151)

当您单击我的导航抽屉中的一个菜单项时,它会变得略暗,并保持这种状态,直到单击另一个项。我试着让它保持同样的颜色不管它在什么状态。
是某种覆盖物吗?我想我已经涵盖了drawer_items. xml中所有可能的状态。
如何防止这种行为?

activity_main.xml

<android.support.design.widget.NavigationView
  .
  .
  android:background="@drawable/drawer_items"
  .
  app:menu="@menu/activity_main_drawer" />

drawer_items.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
  <item android:drawable="@color/grey" android:state_activated="true" />
  <item android:drawable="@color/grey" android:state_selected="true" />
  <item android:drawable="@color/grey" android:state_focused="true"  />
  <item android:drawable="@color/grey" android:state_pressed="true" />
  <item android:drawable="@color/grey" android:state_checked="true" />
  <item android:drawable="@color/grey" />
</selector>

activity_main_drawer.xml

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

    <group android:checkableBehavior="single"
           android:id="@+id/arbitrary_id_1">
      <item
        android:id="@+id/nav_refresh"
        android:icon="@drawable/ic_refresh_24dp"
        android:title="Try Again/Refresh" />
    </group>

    <group android:checkableBehavior="single"
           android:id="@+id/arbitrary_id_2">
      <item
        android:id="@+id/nav_back"
        android:icon="@drawable/ic_fast_rewind_24dp"
        android:title="Go Back" />
    </group>

    <group android:checkableBehavior="single"
           android:id="@+id/arbitrary_id_3">
      <item
        android:id="@+id/nav_3" />
    </group>

</menu>
jgwigjjp

jgwigjjp1#

也许你可以用这个
要防止项目在单击时改变其颜色,您可以尝试在activity_main_drawer.xml中更改:

<group android:checkableBehavior="single">

<group android:checkableBehavior="none">

或者你可以这样做:

<group android:checkableBehavior="single">
    ...
    <item
        android:id="@+id/item_one"
        android:icon="@drawable/ic_item_one"
        android:title="Item One"/>
    ...
  </group>

收件人:

<item>
    <menu>
      ...
      <item
        android:id="@+id/item_one"
        android:icon="@drawable/ic_item_one"
        android:title="Item One"/>
      ...
    </menu>
  </item>

阅读更多Menus

4dbbbstv

4dbbbstv2#

因此,NavigationView基于colorControlHighlight属性突出显示。你可以为你的抽屉创建一个主题,并使其透明:

<style name="Drawer" parent="Theme.AppCompat.Light">
    <item name="colorControlHighlight">#0000</item>
</style>
<android.support.design.widget.NavigationView
    ...
    android:theme="@style/Drawer" />
ruarlubt

ruarlubt3#

尝试在Activity中实现NavigationView.OnNavigationItemSelectedListener并强制return false

override fun onNavigationItemSelected(item: MenuItem): Boolean {
    // Handle navigation view item clicks here.
     when (item.itemId) { 
      }

return false

就像医生说的:

/**
 * Called when an item in the navigation menu is selected.
 *
 * @param item The selected item
 *
 * @return true to display the item as the selected item
 */
public boolean onNavigationItemSelected(@NonNull MenuItem item);
bvhaajcl

bvhaajcl4#

你必须在drawer_items.xml中将所有状态设置为false

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
  <item android:drawable="@color/grey" android:state_activated="false" />
  <item android:drawable="@color/grey" android:state_selected="false" />
  <item android:drawable="@color/grey" android:state_focused="false"  />
  <item android:drawable="@color/grey" android:state_pressed="false" />
  <item android:drawable="@color/grey" android:state_checked="false" />
  <item android:drawable="@color/grey" />
</selector>

相关问题