我正在开发一个社交网络应用,我使用DrawerLayout
和NavigationView
实现了导航抽屉。我使用state-list drawable为菜单中的每个项目定制了背景颜色。问题是,当我在抽屉中选择不同的项目时,项目的按下背景颜色有时会变为蓝色,而不是在drawable中指定的颜色。如果我将设备旋转2-4次并选择不同的项目,但有时会经过并且背景颜色再次出现,则此行为就会消失。这看起来很奇怪,我不知道该怎么办。所以我的问题是:如何防止蓝色出现?
正确按下照片项目的背景色:correct
照片项目的背景颜色不正确:incorrect
我的NavigationView
:
<android.support.design.widget.NavigationView
android:id="@+id/drawer_navigation_view"
android:layout_width="300dp"
android:layout_height="match_parent"
android:layout_gravity="start"
app:headerLayout="@layout/drawer_header"
app:menu="@menu/drawer_menu"
app:itemIconTint="@color/drawer_menu_item_icon_color"
app:itemTextColor="@color/drawer_menu_item_text_color"
app:itemBackground="@drawable/drawer_menu_item_background"/>
drawer_menu_item_background.xml:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_pressed="true"
android:drawable="@color/drawer_menu_item_bg_pressed"/> <!-- #F0F0F0 -->
<item
android:state_checked="true"
android:drawable="@color/drawer_menu_item_bg_checked"/> <!-- #efefef -->
<item
android:drawable="@android:color/transparent"/>
</selector>
编辑:
colors.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="colorPrimary">#44AD79</color>
<color name="register_login_edit_text_color">#222222</color>
<color name="register_login_edit_text_color_hint">#999999</color>
<color name="register_login_button_bg">#eceef1</color>
<color name="register_login_button_bg_pressed">#d2d4d6</color>
<color name="register_login_button_text">#8cdbca</color>
<color name="register_login_button_text_enabled">#26ae90</color>
<color name="darker_bg">#EBEDF0</color>
<color name="drawer_header_bg">#88C973</color>
<color name="drawer_menu_text_color">#303030</color>
<color name="drawer_menu_item_bg_checked">#efefef</color>
<color name="drawer_menu_item_bg_pressed">#F0F0F0</color>
<color name="white">#ffffff</color>
<color name="black">#000000</color>
<color name="light_red">#ff6861</color>
<color name="light_gray">#aaaaaa</color>
<color name="dark_gray">#767676</color>
<color name="greenish">#67b58e</color>
<color name="settings_item_color">#222222</color>
<color name="settings_title_divisor">#d2d4d6</color>
<color name="settings_item_divisor">#EBEDF0</color>
<color name="settings_item_bg_pressed">#eaeef1</color>
<color name="change_pass_edit_text_color_hint">#CDD2D4</color>
<color name="change_pass_edit_text_bottom_color_line">#d2d4d6</color>
<color name="change_pass_button_disabled_bg">#d2d4d6</color>
<color name="change_pass_button_pressed_bg">#72CFA1</color>
<color name="friends_tab_text_color">#D7DEE0</color>
<color name="profile_error_message_button_bg">#CDE6C5</color>
<color name="profile_error_message_button_bg_pressed">#ADDB9E</color>
<color name="profile_error_message_button_text_color">#489E2B</color>
<color name="profile_main_buttons_bg_pressed">#e8e8e8</color>
<color name="profile_wall_divisor">#D7D9DB</color>
</resources>
drawer_menu_item_icon_color.xml:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="@color/colorPrimary" android:state_checked="true"/>
<item android:color="@color/light_gray"/>
</selector>
drawer_menu_item_text_color.xml:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="@color/colorPrimary" android:state_checked="true"/>
<item android:color="@color/drawer_menu_text_color"/>
</selector>