我想建立一個導航抽屜裏的每個項目都有不同的顏色選擇(圖標色彩和文字顏色)爲谷歌Play商店有:更改導航查看商品顏色動態的Android
我不確定他們是如何解決這個問題的,我認爲他們在不同抽屜中使用不同的活動。我想使用片段,我想更改圖標色調和文本顏色。任何想法我可以做到這一點?我正在使用谷歌的設計支持庫和帶有導航視圖的抽屜佈局。
我想建立一個導航抽屜裏的每個項目都有不同的顏色選擇(圖標色彩和文字顏色)爲谷歌Play商店有:更改導航查看商品顏色動態的Android
我不確定他們是如何解決這個問題的,我認爲他們在不同抽屜中使用不同的活動。我想使用片段,我想更改圖標色調和文本顏色。任何想法我可以做到這一點?我正在使用谷歌的設計支持庫和帶有導航視圖的抽屜佈局。
使用app:itemIconTint
在NavigationView
的圖標和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"/>
謝謝,每次點擊菜單項時,我現在都會更改ColorStateList。 –
你會在哪裏放這個選擇器XML片段?此外,'app:itemTextColor'更改所有圖標的顏色。有沒有辦法改變一個圖標的顏色?謝謝! – justinrixx
@justinrixx每次使用以下java代碼選擇一個項目時,我都會更改它:https://jsfiddle.net/a2j7jayo/ –
Finllay我得到了答案,
必須更改colorAccent在顏色的目的文件曾經顏色,你想:
<color name="colorAccent">whichever color required</color>
該解決方案爲我
如果通過動態你的意思以編程方式,你可以試試這個:
// 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。
<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>
這裏有一個簡單的選項。
TL; DR:只需創建一個新的style
與colorPrimary
是您想要的顏色,然後設置NavigationView
的theme
是這個新style
。
只是做一個新的style
爲:
<style name="AppTheme.NoActionBar.NavigationView">
<item name="colorPrimary">@color/myDesiredColor</item>
</style>
這style
會自動從你的基地theme
繼承(在我的情況下,它是AppTheme.NoActionBar
),然後你只需要設置colorPrimary
與您想要的顏色。
然後你只需要設置NavigationView
的theme
爲:
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" />
這是可能的,他們沒有使用NavigationView,因爲這是在以前的應用方式設計支持庫(如果我記得正確)。我認爲你有自己的想法來實現這種效果。 – Longi
@tom。你有沒有得到一個解決方案,你似乎已經接受了答案,但它不適合我。 –