在API 15上,NavigationView菜单项颜色没有变化

时间:2017-04-22 09:33:42

标签: android xml android-layout navigation-drawer

我是StackOverFlow的新手,这是我的第一个问题。如果我犯了任何错误,请原谅我。 我在NavigationViewDrawerLayout看起来像这样

DrawerLayout

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:openDrawer="start">

    <include
        layout="@layout/app_bar_home_screen"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <android.support.design.widget.NavigationView
        android:id="@+id/nav_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="left"
        android:fitsSystemWindows="true"
        android:background="@android:color/white"
        app:itemBackground="@drawable/nav_item_selector"
        app:itemIconTint="@color/nav_icon_color"
        app:itemTextColor="@color/nav_text_color"
        app:headerLayout="@layout/nav_header_home_screen"
        app:menu="@menu/activity_home_screen_drawer">

    </android.support.design.widget.NavigationView>

</android.support.v4.widget.DrawerLayout>

下面是我的nav_item_selector,nav_icon_color,nav_text_color类

nav_item_selector.xml

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@color/fb_color" android:state_pressed="true"/>
    <item android:drawable="@android:color/white" android:state_pressed="false"/>
</selector>

nav_icon_color.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:color="@android:color/white" android:state_pressed="true" />
    <item android:color="#2e9f68" android:state_pressed="false" />
</selector>

nav_text_color.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:color="#b5edd2" android:state_pressed="true" />
    <item android:color="#222222" android:state_pressed="false" />
</selector>

这适用于API&gt; 15但是在API 15上只有itemBackground正在运行,但itemIconTintitemTextColor statePressed无效。不知道为什么。 我还尝试在我的Activity

中以编程方式设置它

MainActivity

navigationView = (NavigationView) findViewById(R.id.nav_view);

int[][] states = new int[][] {
        new int[] { android.R.attr.state_enabled}, // enabled
        new int[] {-android.R.attr.state_enabled}, // disabled
        new int[] {-android.R.attr.state_checked}, // unchecked
        new int[] { android.R.attr.state_pressed}  // pressed
};

int[] colors = new int[] {
        Color.WHITE,
        Color.RED,
        Color.GREEN,
        Color.BLUE
};

navigationView.setItemTextColor(new ColorStateList(states,colors));

下面是API 15和API 19的屏幕截图。我已经在API 16中对此进行了测试,并且工作正常。

在API 15中不起作用:
Not Working in API 15

使用API​​&gt; 15:
Working in API > 15

0 个答案:

没有答案