在系统的preference中,默认布局中包含了一个ImageView、2个TextView和一个LinearLayout布局。在使用Preference的时候,可以通过使用为Preference指定布局来替换原有的系统布局,不过如果只是简单的改变显示布局,可以通过设置setLayoutResource(int layoutId) 函数实现。但是这样做无法对布局中的事件单独设置点击事件。如果需要设置点击事件的话,需要我们自定义Preference。
MainActivity.java
package com.example.custompreferencedemo; import android.preference.Preference; import android.preference.PreferenceActivity; import android.os.Bundle; public class MainActivity extends PreferenceActivity { private Preference _System_Preference; private PreferenceWithCustomId _Custom_Id_Preference; private PreferenceWithSystemId _System_Id_Preference; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); addPreferencesFromResource(R.xml.preference_xml); _InitPreference(); } private void _InitPreference(){ _System_Preference = findPreference("system_preference"); _Custom_Id_Preference = (PreferenceWithCustomId)findPreference("custom_id_preference"); _System_Id_Preference = (PreferenceWithSystemId)findPreference("system_id_preference"); _System_Preference.setLayoutResource(); } }
PreferenceWithSystemId.java
package com.example.custompreferencedemo; import android.content.Context; import android.preference.Preference; import android.util.AttributeSet; import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.ImageView; import android.widget.TextView; import android.widget.Toast; /** * Created by zhuyuqiang on 2016/11/10. */ public class PreferenceWithSystemId extends Preference { private TextView _System_Title; private ImageView _System_Icon; private LayoutInflater _Inflater; private String _Title_Content; public PreferenceWithSystemId(Context context) { this(context,null); } public PreferenceWithSystemId(Context context, AttributeSet attrs) { super(context, attrs); _Inflater = LayoutInflater.from(context); } @Override protected View onCreateView(ViewGroup parent) { super.onCreateView(parent); Log.i("zyq_custom_preference","System_onCreateView"); View mRootView = _Inflater.inflate(R.layout.custom_preference_layout_with_system_id,null); _System_Title = (TextView)mRootView.findViewById(android.R.id.title); _System_Icon = (ImageView)mRootView.findViewById(android.R.id.icon); _System_Icon.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Toast.makeText(getContext(),"_System_Icon",Toast.LENGTH_SHORT).show(); } }); _System_Title.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Toast.makeText(getContext(),"_System_Title",Toast.LENGTH_SHORT).show(); } }); _Title_Content = _System_Title.getText().toString(); return mRootView; } @Override protected void onBindView(View view) { super.onBindView(view); Log.i("zyq_custom_preference","System_onBindView"); } }
PreferenceWithCustomId.java
package com.example.custompreferencedemo; import android.content.Context; import android.preference.Preference; import android.util.AttributeSet; import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.ImageView; import android.widget.TextView; import android.widget.Toast; /** * Created by zhuyuqiang on 2016/11/10. */ public class PreferenceWithCustomId extends Preference { private TextView _Custom_Title; private ImageView _Custom_Icon; private LayoutInflater _Inflater; private String _Title_Content; public PreferenceWithCustomId(Context context) { this(context,null); } public PreferenceWithCustomId(Context context, AttributeSet attrs) { super(context, attrs); _Inflater = LayoutInflater.from(context); } @Override protected View onCreateView(ViewGroup parent) { super.onCreateView(parent); Log.i("zyq_custom_preference","Custom_onCreateView"); View mRootView = _Inflater.inflate(R.layout.custom_preference_layout_with_custom_id,null); _Custom_Title = (TextView)mRootView.findViewById(R.id.custom_title); _Custom_Icon = (ImageView)mRootView.findViewById(R.id.custom_icon); _Custom_Icon.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Toast.makeText(getContext(),"_Custom_Icon",Toast.LENGTH_SHORT).show(); } }); _Custom_Title.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Toast.makeText(getContext(),"_Custom_Title",Toast.LENGTH_SHORT).show(); } }); _Title_Content = _Custom_Title.getText().toString(); return mRootView; } @Override protected void onBindView(View view) { super.onBindView(view); Log.i("zyq_custom_preference","Custom_onBindView"); _Custom_Title.setText(_Title_Content); } }preference_xml.xml
<?xml version="1.0" encoding="utf-8"?> <PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"> <PreferenceCategory> <Preference android:key="system_preference" android:icon="@mipmap/ic_launcher" android:summary="系统Preference" android:title="系统Preference"></Preference> </PreferenceCategory> <PreferenceCategory> <com.example.custompreferencedemo.PreferenceWithCustomId android:key="custom_id_preference"/> </PreferenceCategory> <PreferenceCategory> <com.example.custompreferencedemo.PreferenceWithSystemId android:key="system_id_preference" android:title="System_Title" android:icon="@mipmap/ic_launcher"/> </PreferenceCategory> </PreferenceScreen>custom_preference_layout_with_custom_id.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:minHeight="?android:attr/listPreferredItemHeight" android:orientation="horizontal"> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@mipmap/ic_launcher" /> <TextView android:id="@+id/custom_title" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:gravity="center" android:text="Custom_title" /> <ImageView android:id="@+id/custom_icon" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_gravity="center_horizontal|right" android:layout_marginRight="15dp" android:layout_weight="1" android:src="@mipmap/ic_launcher" /> </LinearLayout>custom_preference_layout_with_system_id.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:minHeight="?android:attr/listPreferredItemHeight" android:orientation="horizontal"> <TextView android:gravity="center" android:id="@android:id/title" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" /> <ImageView android:id="@android:id/icon" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_gravity="center_horizontal|right" android:layout_marginRight="15dp" android:layout_weight="1" /> </LinearLayout>其中布局中如果直接使用android系统id,可以直接在Preference的xml中进行内容设定,如上述代码所示,测试证明,自定义的preference中的点击事件可以响应,不过一般不会这么使用。一般会在自定义的preference中添加一个接口,通过接口在activity中实现布局中控件的点击方法。