Ton*_*lva 24 icons android screen preferences
我正在制作一个扩展PreferenceActivity的应用程序,我想为每个Preference添加一个图标.
我读了一个类似的问题,这是更有声誉的答案:
CommonsWare说:
"设置"应用程序使用私有自定义PreferenceScreen子类来显示图标 - IconPreferenceScreen.这是51行代码,包括注释,但它也需要一些自定义属性.最简单的选择是将所有这些克隆到项目中,即使您不喜欢它.
但我不能让它发挥作用.现在我将类IconPreferenceScreen克隆到我的项目中.我不知道在此之后我要做什么.我正在尝试制作一个新的IconPreferenceScreen我无法使它工作..
IconPreferenceScreen test = new IconPreferenceScreen();
test.setIcon(icon);
Run Code Online (Sandbox Code Playgroud)
Vad*_*dim 38
今天你可以使用自API 11以来可用的android:icon attr :)
Ton*_*lva 19
经过多次测试和许多错误,我可以得到它!
我不得不这样做:
1 -从Android原生设置应用程序克隆类IconPreferenceScreen(感谢CommonWare)
2 -从Android设置应用程序克隆布局文件preference_icon.xml.
3 -在文件attrs.xml中声明IconPreferenceScreen可设置样式:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="IconPreferenceScreen">
<attr name="icon" format="reference" />
</declare-styleable>
</resources>
Run Code Online (Sandbox Code Playgroud)
4 -在preference.xml文件中声明IconPreferenceScreen:
<com.app.example.IconPreferenceScreen
android:title="IconPreferenceScreen Title"
android:summary="IconPreferenceScreen Summary"
android:key="key1" />
Run Code Online (Sandbox Code Playgroud)
5 -最后在首选项类中设置首选项的图标:
addPreferencesFromResource(R.xml.example);
IconPreferenceScreen test = (IconPreferenceScreen) findPreference("key1");
Resources res = getResources();
Drawable icon = res.getDrawable(R.drawable.icon1);
test.setIcon(icono1);
Run Code Online (Sandbox Code Playgroud)
再次感谢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;
}
}
Run Code Online (Sandbox Code Playgroud)
这是克隆的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>
Run Code Online (Sandbox Code Playgroud)
Hen*_*cha 11
实现您想要的最佳和最简单的方法是使图标成为带有右边框作为可伸展区域的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;
}
}
Run Code Online (Sandbox Code Playgroud)
由于图标是9补丁,图标将拉伸透明部分直到单元格的右端,将图标放在左侧.
这是一个非常干净的解决方案.
太迟了,但也许会帮助某人的问题是,您可以添加图标的唯一子类,例如这是我的代码示例。屏幕上只能看到编辑文本图标。
<?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>
Run Code Online (Sandbox Code Playgroud)
结果:
归档时间: |
|
查看次数: |
31642 次 |
最近记录: |