Customize preferecnce with icons -- listpreference class implementation (1)

Source: Internet
Author: User

Sometimes the preference provided by the system does not meet our requirements, so sometimes we need to customize preferece. The following example is a simple preference with icons customized by myself.

The first is the XML layout file, which is the layout you want to implement.

<?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="fill_parent"    android:layout_height="wrap_content"    android:minHeight="?android:attr/listPreferredItemHeight"    android:gravity="center_vertical"    android:paddingRight="?android:attr/scrollbarSize">       <RelativeLayout        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_marginLeft="18dip"        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>    <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" /></LinearLayout> 

The following is the custom preference Java file:


Import COM. android. MMS. r; import android. content. context; import android. graphics. drawable. drawable; import android. preference. preference; import android. util. attributeset; import android. view. view; import android. widget. imageview; public class iconlistpreference extends preference {private drawable micon; Public iconlistpreference (final context, final attributeset attrs, final int defstyle) {supe R (context, attrs); this. setlayoutresource (R. layout. icon_list_preference); // The icon initialized by the icon this is set here. micon = context. getresources (). getdrawable (R. drawable. ycz20_black);} public iconlistpreference (final context, final attributeset attrs) {This (context, attrs, 0);} @ override protected void onbindview (final view) {super. onbindview (View); Final imageview = (imageview) view. findv Iewbyid (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. policychanged () ;}} public void seticon (INT iconres) {If (R. drawable. ycz20_black! = Iconres) {This. micon = getcontext (). getresources (). getdrawable (iconres); this. policychanged () ;}}/*** returns the icon of this preference. ** @ return the icon. * @ see # seticon (drawable) */Public drawable getIcon () {return this. micon ;}}


For example, you want to change the icon in the imageview by using the seticon () method.

Because the functions you have completed are relatively simple, there are fewer rewrite methods. You can add more methods as needed.

But it only implements a preferecnce, but how to implement a function similar to listpreference, but it is more elegant than listpreference? Please refer to the next article!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.