2017-03-16 54 views
0

我爲所有偏好設置了平面圖標,我想更改全局該圖標的顏色。如何更改全局偏好圖標顏色Android

當我嘗試下面的代碼時,它甚至會更改工具欄中的後退按鈕顏色。

我只希望偏好圖標色調在全球範圍內更改。預先感謝。

<?xml version="1.0" encoding="utf-8"?> 
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"> 

    <SwitchPreference 
     android:id="@+id/pref_toggle_alarm" 
     android:icon="@drawable/ic_pref_notifications" 
     android:key="key_toggle_alarm" 
     android:summaryOff="Alarm OFF" 
     android:summaryOn="Alarm ON" 
     android:title="Alarm" /> 


    <web.prefs.TimePrefs 
     android:id="@+id/pref_select_time" 
     android:icon="@drawable/ic_pref_time" 
     android:key="key_time" 
     android:summary="Set some time" 
     android:title="Select Time" /> 

    <MultiSelectListPreference 
     android:id="@+id/pref_select_week" 
     android:defaultValue="@array/week_array_values" 
     android:entries="@array/array_week_selection" 
     android:entryValues="@array/week_array_values" 
     android:icon="@drawable/ic_pref_time" 
     android:key="key_week" 
     android:title="Select Days" /> 

    <ListPreference 
     android:id="@+id/pref_track" 
     android:defaultValue="0" 
     android:entries="@array/tracks_arrays" 
     android:entryValues="@array/tracks_arrays_values" 
     android:icon="@drawable/ic_music_note" 
     android:key="key_track" 
     android:summary="%s" 
     android:title="Select Track" /> 

</PreferenceScreen> 

style.xml

<style name="PreferencesTheme" parent="@style/AppTheme.NoActionBar"> 
    <item name="android:textColorPrimary">@color/primary_text</item> 
    <item name="android:textColorSecondary">@color/secondary_text</item> 
    <item name="android:colorAccent">@color/accent</item> 
    <item name="android:tint">@color/accent</item> 
</style> 
+0

它們會同步更改,因爲它們使用相同的命名顏色,嘗試將不同的顏色設置爲偏好項目背景, @ color/your_color' –

回答

2

不幸的是,沒有簡單的方法輕鬆染成偏好圖標。我的解決方案,如果您使用矢量圖標是爲了將屬性集成到每個圖像的XML中。

在值/ attrs.xml:

<?xml version="1.0" encoding="utf-8"?> 
<resources> 
    <attr name="iconPreferenceColor" format="reference|color" /> 
</resources> 

在每個圖標添加機器人: 「?ATTR/iconPreferenceColor」 填充顏色=,樣品:

<vector xmlns:android="http://schemas.android.com/apk/res/android" 
     android:width="24dp" 
     android:height="24dp" 
     android:viewportWidth="24.0" 
     android:viewportHeight="24.0"> 
    <path 
     android:fillColor="?attr/iconPreferenceColor" 
     android:pathData="M13,2.05v3.03c3.39,0.49 6,3.39 6,6.92 0,0.9 -0.18,1.75 -0.48,2.54l2.6,1.53c0.56,-1.24 0.88,-2.62 0.88,-4.07 0,-5.18 -3.95,-9.45 -9,-9.95zM12,19c-3.87,0 -7,-3.13 -7,-7 0,-3.53 2.61,-6.43 6,-6.92V2.05c-5.06,0.5 -9,4.76 -9,9.95 0,5.52 4.47,10 9.99,10 3.31,0 6.24,-1.61 8.06,-4.09l-2.6,-1.53C16.17,17.98 14.21,19 12,19z"/> 
</vector> 

和在款式:

<item name="iconPreferenceColor">@color/green</item> 

我希望它有幫助