How to add icons to PreferenceScreen entries
C开发者_开发百科onsider this preferences.xml file:
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen
xmlns:android="http://schemas.android.com/apk/res/android"
android:title="@string/preference_main">
<PreferenceScreen
android:title="@string/preference_sight"
android:key="category_sight">
<ListPreference
android:summary="@string/preference_sight_wb_msg"
android:title="@string/preference_sight_wb_title"
android:key="sight_wb" android:defaultValue="auto"/>
<Preference
android:key="sight_wb_values_cache"/>
<eu.elevelcbt.sm.preferences.PercentBarPreference
android:title="@string/preference_sight_mean_confidence_min_title"
android:summary="@string/preference_sight_mean_confidence_min_msg"
android:key="sight_mean_confidence_min"
android:defaultValue="80"/>
<CheckBoxPreference
android:key="sight_flash"
android:defaultValue="false"
android:summary="@string/preference_sight_flash_msg"
android:title="@string/preference_sight_flash_title"/>
</PreferenceScreen>
</PreferenceScreen>
When shown in my MainPreference class extending PreferenceActivity is correctly shows me a first level menu with one entry "Sight" (@string/preference_main) which, when selected, takes me to the second preference screen where I have all my preferences. Everything works as I wanted. The only thing is the on first preference screen I want to put an icon beside the label "Sight" like in main Android setting menu.
How can I do that? Thank you very much in advance for any help! Luca.
Mine:
Desired:
...mmm I tried but no luck...
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen
xmlns:android="http://schemas.android.com/apk/res/android"
android:title="@string/preference_main">
<PreferenceScreen
android:title="@string/preference_sight"
android:key="category_sight"
android:icon="@drawable/ic_dialog_light">
...
</PreferenceScreen>
where am I doing wrong?
I have another suggestion which works for me on my Nexus 7, running Android 4.2, and my ASUS TF101, running Android 4.0.3: add the icon attribute to the preference headers, like so:
<?xml version="1.0" encoding="utf-8"?>
<preference-headers xmlns:android="http://schemas.android.com/apk/res/android" >
<header
android:fragment="com.example.SettingsFragment"
android:summary="@string/pref_summary_caching"
android:icon="@drawable/ic_pref_cache"
android:title="@string/pref_cat_caching" >
<extra
android:name="category"
android:value="caching" />
</header>
</preference-headers>
this lets the icons appear to the left of the header text. Seems to be documented nowhere, and you cannot enter the values through the Structure screen of the XML editor, as there does not seem to be an XML schema for the preference-headers file. But hey, it WORKS!
Use android:icon
http://developer.android.com/reference/android/preference/Preference.html#attr_android:icon
You can look at the source code to the settings app and see they they are extending Preference
to do this. Here is the class file.
https://github.com/android/platform_packages_apps_settings/blob/master/src/com/android/settings/IconPreferenceScreen.java
hope this helps!
Just add android:icon="@mipmap/icon_launcher
It works.
Example code:
<ListPreference
android:icon="@mipmap/icon_launcher"
android:title="@string/pref_sort_order_label"
android:key="@string/pref_sort_order_key"
android:defaultValue="@string/pref_most_popular"
android:entries="@array/pref_sort_order"
android:entryValues="@array/pref_sort_order">
</ListPreference>
Old question but the answer is now in the developers website: http://developer.android.com/guide/topics/ui/settings.html#PreferenceHeaders
icon
A Drawable that represents the Preference icon.
Example: app:icon="@drawable/ic_camera"
https://developer.android.com/guide/topics/ui/settings/components-and-attributes?hl=vi
精彩评论