2015-06-21 80 views
13

我想建立一個導航抽屜裏的每個項目都有不同的顏色選擇(圖標色彩和文字顏色)爲谷歌Play商店有:更改導航查看商品顏色動態的Android

enter image description here

我不確定他們是如何解決這個問題的,我認爲他們在不同抽屜中使用不同的活動。我想使用片段,我想更改圖標色調和文本顏色。任何想法我可以做到這一點?我正在使用谷歌的設計支持庫和帶有導航視圖的抽屜佈局。

+1

這是可能的,他們沒有使用NavigationView,因爲這是在以前的應用方式設計支持庫(如果我記得正確)。我認爲你有自己的想法來實現這種效果。 – Longi

+0

@tom。你有沒有得到一個解決方案,你似乎已經接受了答案,但它不適合我。 –

回答

33

使用app:itemIconTintNavigationView的圖標和textColors

樣品使用app:itemTextColor

drawable/navigation_text_color

<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
    <!-- This is used when the Navigation Item is checked --> 
    <item android:color="#009688" android:state_checked="true" /> 
    <!-- This is the default text color --> 
    <item android:color="#E91E63" /> 
</selector> 

layout

<android.support.design.widget.NavigationView 
     . 
     . 
     app:itemTextColor="@drawable/navigation_text_color"/> 
+0

謝謝,每次點擊菜單項時,我現在都會更改ColorStateList。 –

+0

你會在哪裏放這個選擇器XML片段?此外,'app:itemTextColor'更改所有圖標的顏色。有沒有辦法改變一個圖標的顏色?謝謝! – justinrixx

+1

@justinrixx每次使用以下java代碼選擇一個項目時,我都會更改它:https://jsfiddle.net/a2j7jayo/ –

0

Finllay我得到了答案,

必須更改colorAccent在顏色的目的文件曾經顏色,你想:

<color name="colorAccent">whichever color required</color> 

該解決方案爲我

5

如果通過動態你的意思以編程方式,你可以試試這個:

// FOR NAVIGATION VIEW ITEM TEXT COLOR 
int[][] states = new int[][]{ 
     new int[]{-android.R.attr.state_checked}, // unchecked 
     new int[]{android.R.attr.state_checked}, // checked 
     new int[]{}        // default 
}; 

// Fill in color corresponding to state defined in state 
int[] colors = new int[]{ 
     Color.parseColor("#747474"), 
     Color.parseColor("#007f42"), 
     Color.parseColor("#747474"), 
}; 

ColorStateList navigationViewColorStateList = new ColorStateList(states, colors); 

// apply to text color 
navigationView.setItemTextColor(navigationViewColorStateList); 

// apply to icon color 
navigationView.setItemIconTintList(navigationViewColorStateList); 

所以你可以爲不同的設置定義多種顏色l ike Day或Night。

0
<android.support.design.widget.NavigationView 
      . 
      . 
      app:itemIconTint="@color/your_selector_file_name" // for Icon 
     app:itemTextColor="@color/your_selector_file_name" // for text 
/> 

添加到@DAVOOD所說的內容。下面的顏色選擇器適合我。

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
     <!-- This is used when the Navigation Item is checked --> 
     <item android:color="@color/tenoBrandColor" android:state_pressed="true" /> 
     <!-- This is the default text color --> 
     <item android:color="@color/textprimary" /> 
</selector> 
0

這裏有一個簡單的選項。

TL; DR:只需創建一個新的stylecolorPrimary是您想要的顏色,然後設置NavigationViewtheme是這個新style

只是做一個新的style爲:

<style name="AppTheme.NoActionBar.NavigationView"> 
    <item name="colorPrimary">@color/myDesiredColor</item> 
</style> 

style會自動從你的基地theme繼承(在我的情況下,它是AppTheme.NoActionBar),然後你只需要設置colorPrimary與您想要的顏色。

然後你只需要設置NavigationViewtheme爲:

android:theme="@style/AppTheme.NoActionBar.NavigationView" 

在:

<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/nav_header_menu_test" 
    app:menu="@menu/main_drawer" 
    android:theme="@style/AppTheme.NoActionBar.NavigationView" />