Как добавить значки в Предпочтения
Я создаю приложение, которое расширяет PreferenceActivity, и я хочу добавить значок для каждого параметра.
Я прочитал аналогичный вопрос, и это ответ с большей репутацией:
Сообщество CommonsWare:
В приложении "Настройки" используется специальный пользовательский подкласс PreferenceScreen, чтобы иметь значок - IconPreferenceScreen. Это 51 строка кода, включая комментарии, хотя для этого также требуются некоторые пользовательские атрибуты. Самый простой вариант - клонировать все это в ваш проект, даже если вам это не нравится.
Но я не могу заставить его работать. На данный момент я клонировал класс IconPreferenceScreen в свой проект. И я не знаю, что мне делать после этого. Я пытаюсь создать новый IconPreferenceScreen, я не могу заставить его работать.
IconPreferenceScreen test = new IconPreferenceScreen();
test.setIcon(icon);
Ответы
Ответ 1
После многих тестов и многих ошибок я смог его получить!
Я должен был сделать это:
1 - Клонировать класс IconPreferenceScreen из собственного приложения настроек Android (спасибо CommonWare)
2 - Скройте файл макета preference_icon.xml из приложения Android Settings.
3 - Объявите стиль IconPreferenceScreen в файле attrs.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="IconPreferenceScreen">
<attr name="icon" format="reference" />
</declare-styleable>
</resources>
4 - Объявите IconPreferenceScreen в файле preference.xml:
<com.app.example.IconPreferenceScreen
android:title="IconPreferenceScreen Title"
android:summary="IconPreferenceScreen Summary"
android:key="key1" />
5 - Наконец, установите значок для предпочтения в классе предпочтений:
addPreferencesFromResource(R.xml.example);
IconPreferenceScreen test = (IconPreferenceScreen) findPreference("key1");
Resources res = getResources();
Drawable icon = res.getDrawable(R.drawable.icon1);
test.setIcon(icono1);
Еще раз спасибо CommonsWare за рассказ, с чего начать, и за его объяснения.
Это клонированный класс IconPreferenceScreen:
package com.app.example;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.preference.Preference;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;
public class IconPreferenceScreen extends Preference {
private Drawable mIcon;
public IconPreferenceScreen(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public IconPreferenceScreen(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
setLayoutResource(R.layout.preference_icon);
TypedArray a = context.obtainStyledAttributes(attrs,
R.styleable.IconPreferenceScreen, defStyle, 0);
mIcon = a.getDrawable(R.styleable.IconPreferenceScreen_icon);
}
@Override
public void onBindView(View view) {
super.onBindView(view);
ImageView imageView = (ImageView) view.findViewById(R.id.icon);
if (imageView != null && mIcon != null) {
imageView.setImageDrawable(mIcon);
}
}
public void setIcon(Drawable icon) {
if ((icon == null && mIcon != null) || (icon != null && !icon.equals(mIcon))) {
mIcon = icon;
notifyChanged();
}
}
public Drawable getIcon() {
return mIcon;
}
}
И это клонированный шаблон preference_icon.xml:
<LinearLayout android:id="@+android:id/iconpref"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="?android:attr/listPreferredItemHeight"
android:gravity="center_vertical"
android:paddingRight="?android:attr/scrollbarSize">
<ImageView android:id="@+id/icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="6dip"
android:layout_marginRight="6dip"
android:layout_gravity="center" />
<RelativeLayout android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="2dip"
android:layout_marginRight="6dip"
android:layout_marginTop="6dip"
android:layout_marginBottom="6dip"
android:layout_weight="1">
<TextView android:id="@+android:id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:singleLine="true"
android:textAppearance="?android:attr/textAppearanceLarge"
android:ellipsize="marquee"
android:fadingEdge="horizontal" />
<TextView android:id="@+android:id/summary"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@android:id/title"
android:layout_alignLeft="@android:id/title"
android:textAppearance="?android:attr/textAppearanceSmall"
android:maxLines="2" />
</RelativeLayout>
</LinearLayout>
Ответ 2
Сегодня вы можете просто использовать андроид: значок attr, доступный с API 11:)
Ответ 3
Самый лучший и простой способ добиться того, что вы хотите, - сделать значок значком с 9 патчами с правой границей в качестве растяжимой области.
Скажем, у вас есть EditTextPreference, который вы хотите добавить значок перед заголовком и резюме.
Вы создаете класс MyEditTextPreference, который расширяет EditTextPreference и переопределяет метод getView, чтобы добавить значок вашего 9-патча в качестве фонового ресурса.
Вот пример кода, который работает:
public class MyEditTextPreference extends EditTextPreference {
public MyEditTextPreference(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
public View getView(View convertView, ViewGroup parent) {
View view = super.getView(convertView, parent);
view.setBackgroundResource(R.drawable.my_icon);
return view;
}
}
Поскольку значок представляет собой 9-патч, значок растягивает прозрачную часть до правого конца ячейки, помещая значок слева.
Это очень чистое решение для вашей проблемы.
Ответ 4
Я делаю приложение в том, что PreferenceActivity является основным видом деятельности, и я хочу добавить значок к каждому пользовательскому предпочтению. Как это изображение:
Это не a PreferenceActivity
.
Как я могу добавить значок на экран предпочтений?
Вам будет проще просто создать свою собственную деятельность, которая происходит для сохранения настроек.
Лично я просто пропустил значок и воспользуюсь регулярной системой PreferenceScreen
и PreferenceActivity
.
Ответ 5
Благодаря:
Следующие работали для меня:
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest
xmlns:android="http://schemas.android.com/apk/res/android"
package="YOUR.PACKAGE.NAME">
<application
android:label="@string/app_name"
android:icon="@drawable/icon">
<activity
android:name="AndroidMain"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
RES/значения/strings.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2">
<string name="app_name">YOUR_APP_NAME</string>
<string name="about">About</string>
</resources>
RES/значения/attrs.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="IconPreference">
<attr name="icon" format="reference" />
</declare-styleable>
</resources>
Рез/макет/main.xml:
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:settings="http://schemas.android.com/apk/res/YOUR.PACKAGE.NAME"
>
<PreferenceCategory
android:title="Main"
android:key="mainPrefCat">
<YOUR.PACKAGE.NAME.IconPreference
android:title="Exit"
android:summary="Quit the app."
settings:icon="@drawable/YOUR_ICON"
android:key="quitPref">
</YOUR.PACKAGE.NAME.IconPreference>
</PreferenceCategory>
</PreferenceScreen>
Рез/макет/preference_icon.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+android:id/widget_frame"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="?android:attr/listPreferredItemHeight"
android:gravity="center_vertical"
android:paddingRight="?android:attr/scrollbarSize">
<ImageView
android:id="@+id/icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="6dip"
android:layout_marginRight="6dip"
android:layout_gravity="center" />
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="2dip"
android:layout_marginRight="6dip"
android:layout_marginTop="6dip"
android:layout_marginBottom="6dip"
android:layout_weight="1">
<TextView android:id="@+android:id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:singleLine="true"
android:textAppearance="?android:attr/textAppearanceLarge"
android:ellipsize="marquee"
android:fadingEdge="horizontal" />
<TextView android:id="@+android:id/summary"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@android:id/title"
android:layout_alignLeft="@android:id/title"
android:textAppearance="?android:attr/textAppearanceSmall"
android:maxLines="2" />
</RelativeLayout>
</LinearLayout>
Значки соответствующего размера:
res/drawable-hdpi/YOUR_ICON.png
res/drawable-mdpi/YOUR_ICON.png
res/drawable-ldpi/YOUR_ICON.png
AndroidMain.scala:
class AndroidMain extends PreferenceActivity {
override def onCreate(savedInstanceState: Bundle) {
super.onCreate(savedInstanceState)
addPreferencesFromResource(R.layout.main)
}
}
IconPreference.scala:
class IconPreference(context: Context, attrs: AttributeSet, defStyle: Int) extends Preference(context, attrs, defStyle) {
def this(context: Context, attrs: AttributeSet) = this(context, attrs, 0)
setLayoutResource(R.layout.preference_icon);
val a: TypedArray = context.obtainStyledAttributes(attrs, R.styleable.IconPreference, defStyle, 0);
val _icon: Drawable = a.getDrawable(R.styleable.IconPreference_icon);
override def onBindView(view: View) {
super.onBindView(view)
val imageView: ImageView = view.findViewById(R.id.icon).asInstanceOf[ImageView]
if (imageView != null && _icon != null) {
imageView.setImageDrawable(_icon);
}
}
}
Ответ 6
Для ответа "здравомыслящий" вы можете использовать метод 9-patch, как описано Henrique Rocha, или вы можете использовать следующее:
Создайте новый класс предпочтений, который расширяет EditTextPreference и позволяет ему переопределять onCreateView.
@Override
protected View onCreateView(ViewGroup parent)
{
View v = LayoutInflater.from(getContext()).inflate(R.layout.iconpreference, null);
return v;
}
Создайте новый XML-формат Layout, названный iconpreference, и в этом XML вы можете включить его в стандартную компоновку предпочтений. Вы можете основательно настроить его так, как хотите.
Простой пример:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<ImageView android:layout_width="48dp"
android:layout_height="48dp"
android:src="@drawable/checkmark_black"/>
<include layout="@layout/preference_holo" />
</LinearLayout>
Вот и все!
Ответ 7
Когда вам понадобится listpreference с {text и icon}, вы можете посмотреть этот список с иконками
Ответ 8
слишком поздно, но, возможно, это поможет кому-то в том, что проблема заключается в том, что вы можете добавить только дочерний значок значка категории, такой как это мои образцы кода. на экране можно увидеть только текстовый значок.
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<PreferenceCategory
android:title="@string/pref_profil"
android:icon="@drawable/profile" // it wont show
android:key="pref_key_storage_settings">
<EditTextPreference
android:key="edit_text_preference_1"
android:selectAllOnFocus="true"
android:singleLine="true"
android:icon="@drawable/profile"
android:title="Edit text preference" />
<Preference
android:key="pref_key_sms_delete_limit"
android:summary="HELLO2"
android:title="HELLO" />
</PreferenceCategory>
</PreferenceScreen>
результат:
![введите описание изображения здесь]()
Ответ 9
Попробуйте следующее:
PreferenceScreen root = getPreferenceManager().createPreferenceScreen(this);
PreferenceScreen ps= getPreferenceManager().createPreferenceScreen(this);
ps.setKey("ps");
ps.setLayoutResource(R.layout.your_layout);
root.addPreference(ps);