Sai*_*Sai 3 android android-preferences
我已经为所有“首选项”设置了平面图标,我想全局更改该图标的颜色。
当我尝试以下代码时,它甚至会更改工具栏中的后退按钮颜色。
我只希望全局更改“首选项”图标的色调。预先感谢。
<?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>
Run Code Online (Sandbox Code Playgroud)
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>
Run Code Online (Sandbox Code Playgroud)
您必须以编程方式更改首选项图标的颜色,无法通过主题或 XML 属性来完成此操作。您可以在您的 中添加以下内容PreferenceFragment:
@Override
public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
addPreferencesFromResource(R.xml.preferences);
int colorAttr = android.R.attr.textColorSecondary;
TypedArray ta = context.getTheme().obtainStyledAttributes(new int[]{colorAttr});
int iconColor = ta.getColor(0, 0);
ta.recycle();
tintIcons(getPreferenceScreen(), iconColor);
}
private static void tintIcons(Preference preference, int color) {
if (preference instanceof PreferenceGroup) {
PreferenceGroup group = ((PreferenceGroup) preference);
for (int i = 0; i < group.getPreferenceCount(); i++) {
tintIcons(group.getPreference(i), color);
}
} else {
Drawable icon = preference.getIcon();
if (icon != null) {
DrawableCompat.setTint(icon, color);
}
}
}
Run Code Online (Sandbox Code Playgroud)
另外,我认为这个库也可以帮助您为图标着色。它还修复了 AppCompat 首选项的其他问题。
不幸的是,没有简单的方法可以设置首选项图标。我的解决方案(如果使用矢量图标)是使一个属性集成到每个图像的XML中。
在values / attrs.xml中:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<attr name="iconPreferenceColor" format="reference|color" />
</resources>
Run Code Online (Sandbox Code Playgroud)
在每个图标中添加android:fillColor =“?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>
Run Code Online (Sandbox Code Playgroud)
和风格:
<item name="iconPreferenceColor">@color/green</item>
Run Code Online (Sandbox Code Playgroud)
希望对您有所帮助
| 归档时间: |
|
| 查看次数: |
1790 次 |
| 最近记录: |