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; } }
<?xml version="1.0" encoding="utf-8"?> <resources> <declare-styleable name="IconPreferenceScreen"> <attr name="icon" format="reference" /> </declare-styleable> </resources>
<?xml version="1.0" encoding="utf-8"?> <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>
addPreferencesFromResource(R.xml.preferences); IconPreferenceScreen test = (IconPreferenceScreen) findPreference("key"); Resources res = getResources(); Drawable icon = res.getDrawable(R.drawable.icon1); test.setIcon(icono1);
<com.example.app.IconPreferenceScreen android:title="IconPreferenceScreen" android:key="key" />
test.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() { @Override public boolean onPreferenceClick(Preference preference) { startActivity(new Intent(PreferencesActivity.this, PreferencesActivity2.class)); return true; } });
android:theme="@android:style/Theme.Dialog"
<?xml version="1.0" encoding="utf-8"?> <resources> <style name="NoTitleDialog" parent="android:style/Theme.Dialog"> <item name="android:windowNoTitle">true</item> </style> </resources>
android:theme="@style/NoTitleDialog"
Source: https://habr.com/ru/post/125259/
All Articles