Navigation Drawer item background colour for selected item

后端 未结 13 1382
独厮守ぢ
独厮守ぢ 2020-11-28 23:54

I used Android Studio to implement the Navigation Drawer and I can\'t get the blue colour that is used to show which section we\'re currently in to change.

I\'ve tri

相关标签:
13条回答
  • 2020-11-29 00:13

    Still not sure why it is that it doesn't work. But the way I found around it is to use my own simple_list_item_activated layout to be passed to the ArrayAdapter which was basically the same except for setting the text colour to white. I then replaced getActionBar().getThemedContext() with getActivity().getBaseContext() and it now has an effect.

    This may not be the correct way and may have repercussions in the future, but for now I have it working the way I want it to.

    0 讨论(0)
  • 2020-11-29 00:13

    This worked for me : implemented menu drawer not by populating the navigation view with list, but with menu items.

    created a drawable like this :

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

    and in onCreate method , set the id of the menu item which corresponds to selected activity as checked. and implement the drawable selector file to your navigation drawer like this

        app:itemBackground="@drawable/drawer_menu_selector"
    

    fyi : need to define your 'app' namespace.

    0 讨论(0)
  • 2020-11-29 00:13

    I searched for solution on this issue, tried everything, and only this solution worked for me.

    You can find it on this link https://tuchangwei.github.io/2016/07/18/The-solution-that-the-menu-item-of-Navigation-View-can-t-change-its-background-When-it-is-selected-checked/

    All the credit to https://tuchangwei.github.io/

    0 讨论(0)
  • 2020-11-29 00:16

    This is working for me:

    1. First define a drawable item_bg.xml as:

      <?xml version="1.0" encoding="utf-8"?>
          <selector xmlns:android="http://schemas.android.com/apk/res/android">
              <item android:state_checked="true" android:drawable="@drawable/nav_menu_bg" />
          </selector>
      
    2. Then use this drawable in navigation_main_layout.xml as:

      <android.support.design.widget.NavigationView
          android:id="@+id/nav_view"
          android:layout_width="wrap_content"
          android:layout_height="match_parent"
          android:layout_gravity="start"
          app:itemBackground="@drawable/item_bg"
          android:fitsSystemWindows="true"
          app:headerLayout="@layout/nav_header_navigation"
          app:menu="@menu/navigation_main_layout_drawer" />
      
    0 讨论(0)
  • 2020-11-29 00:17

    In Future if anyone comes here using, Navigation Drawer Activity (provided by Studio in Activity Prompt window)

    The answer is -

    Use this before OnCreate() in MainActivity

        int[][] state = new int[][] {
                new int[] {android.R.attr.state_checked}, // checked
                new int[] {-android.R.attr.state_checked}
        };
    
        int[] color = new int[] {
                Color.rgb(255,46,84),
                (Color.BLACK)
        };
    
        ColorStateList csl = new ColorStateList(state, color);
    
        int[][] state2 = new int[][] {
                new int[] {android.R.attr.state_checked}, // checked
                new int[] {-android.R.attr.state_checked}
        };
    
        int[] color2 = new int[] {
                Color.rgb(255,46,84),
                (Color.GRAY)
        };
    
    ColorStateList csl2 = new ColorStateList(state2, color2);
    

    and use this in onNavigationItemSelected() in MainActivity (you dont need to Write this function if you use Navigation Drawer activity, it will be added in MainActivity).

     NavigationView nav = (NavigationView) findViewById(R.id.nav_view);
        nav.setItemTextColor(csl);
        nav.setItemIconTintList(csl2);
        nav.setItemBackgroundResource(R.color.white);
    

    Tip - add this code before If else Condition in onNavigationItemSelected()

    0 讨论(0)
  • 2020-11-29 00:18

    To change the "Navigation Drawer item background colour for selected item" you could do it with the attribut:

    colorControlHighlight

    In your "styles.xml" it could look like this:

    <style name="YourStyleNameFor.NavigationDrawer" parent="ThemeOverlay.AppCompat.Light">
        <item name="colorControlHighlight">@color/your_highlight_color</item>
    </style>
    

    Don't forget to apply your Style in the tag:

    android.support.design.widget.NavigationView

    For example in your activity_main.xml it could look like this:

    <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:headerLayout="@layout/navigationdrawer_header"
    
        <!-- the following line referes to your style  -->
        app:theme="@style/YourThemeNameFor.NavigationDrawer"
    
        <!-- the following two lines are maybe also interesting for you -->
        app:itemIconTint="@color/gray3"
        app:itemTextColor="@color/gray3"
    
        app:menu="@menu/navigationdrawer_main" />
    
    0 讨论(0)
提交回复
热议问题