IconCheckBoxPreference: 一個帶有表徵圖的CheckBox preference

來源:互聯網
上載者:User
這篇文章原文是在https://gist.github.com/515681。主要是代碼,沒有其他文字說明,轉到這裡來,希望對看到的人有用。下面是用到的xml布局檔案。
<?xml version="1.0" encoding="UTF-8"?><LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"    android:id="@+android:id/widget_frame"    android:layout_width="fill_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>    <CheckBox     android:id="@+android:id/checkbox"     android:layout_width="wrap_content"     android:layout_height="wrap_content"     android:layout_marginRight="4dip"     android:layout_gravity="center_vertical"     android:focusable="false"     android:clickable="false" /></LinearLayout> 

下面是用到的java類檔案。

import android.content.Context;import android.graphics.drawable.Drawable;import android.preference.CheckBoxPreference;import android.util.AttributeSet;import android.view.View;import android.widget.ImageView;import com.jakewharton.wakkawallpaper.R;public class IconCheckBoxPreference extends CheckBoxPreference {    private Drawable mIcon;        public IconCheckBoxPreference(final Context context, final AttributeSet attrs, final int defStyle) {        super(context, attrs, defStyle);                this.setLayoutResource(R.layout.icon_checkbox_preference);                this.mIcon = context.obtainStyledAttributes(attrs, R.styleable.IconPreference, defStyle, 0).getDrawable(R.styleable.IconPreference_icon);    }    public IconCheckBoxPreference(final Context context, final AttributeSet attrs) {        this(context, attrs, 0);    }    @Override    protected void onBindView(final View view) {        super.onBindView(view);                final ImageView imageView = (ImageView)view.findViewById(R.id.icon);        if ((imageView != null) && (this.mIcon != null)) {            imageView.setImageDrawable(this.mIcon);        }    }    /*** Sets the icon for this Preference with a Drawable.** @param icon The icon for this Preference*/    public void setIcon(final Drawable icon) {        if (((icon == null) && (this.mIcon != null)) || ((icon != null) && (!icon.equals(this.mIcon)))) {            this.mIcon = icon;            this.notifyChanged();        }    }    /*** Returns the icon of this Preference.** @return The icon.* @see #setIcon(Drawable)*/    public Drawable getIcon() {        return this.mIcon;    }}

如果有需要做類似功能的朋友們可以參照此例進行修改。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.