由于系统自带的Preference只支持CheckBoxPreference ListPreference editPreference等几种偏好选项,假如我们需要在Preference中添加SeekBar来实现一个音量控制条的功能,这时候就需要自定义一个类.
这里我们通过继承DialogPreference来实现.
首先定义一个xml布局文件来描述seekbar的布局
seek.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:orientation="vertical" >
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:text="111" />
<TextView
android:id="@+id/text2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:text="222" />
</RelativeLayout>
<SeekBar
android:id="@+id/seekbar1"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/text3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="333" />
</LinearLayout>
接着我们需要定义2个seekbar的属性,minValue和maxValue来分别描述进度条的最小值和最大值.
我们知道自定义控件的属性需要在attrs.xml中声明:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="com.example.dialogpreferencesseekbar.SeekBarPreferences">
<attr name="minValue" format="integer" />
<attr name="maxValue" format="integer" />
</declare-styleable>
</resources>
再来看看我们自定义SeekBarPreference继承于DialogPreference的类SeekBarPreferences.java:
package com.example.dialogpreferencesseekbar;
import android.content.Context;
import android.content.res.TypedArray;
import android.preference.DialogPreference;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView;
public class SeekBarPreferences extends DialogPreference implements
OnSeekBarChangeListener {
private static final String PREFERENCE_NS = "http://schemas.android.com/apk/res/com.example.dialogpreferencesseekbar";
private static final String ANDROID_NS = "http://schemas.android.com/apk/res/android";
private static final String ATTR_DEFAULT_VALUE = "defaultValue";
private static final String ATTR_MIN_VALUE = "minValue";
private static final String ATTR_MAX_VALUE = "maxValue";
private int mMinValue;
private int mMaxValue;
private int mDefaultValue;
private int mCurrentValue;
private SeekBar mSeekBar;
private TextView mValueText;
public SeekBarPreferences(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
mMinValue = attrs
.getAttributeIntValue(PREFERENCE_NS, ATTR_MIN_VALUE, 0);
mMaxValue = attrs.getAttributeIntValue(PREFERENCE_NS, ATTR_MAX_VALUE,
100);
/********也可以通过Context的obtainStyledAttributes获得自定义的属性值***********/
// TypedArray array = context.obtainStyledAttributes(attrs,
// R.styleable.com_example_dialogpreferencesseekbar_SeekBarPreferences);
// mMaxValue = array.getInteger(R.styleable.com_example_dialogpreferencesseekbar_SeekBarPreferences_maxValue, 100);
// mMinValue = array.getInteger(R.styleable.com_example_dialogpreferencesseekbar_SeekBarPreferences_minValue, 0);
mDefaultValue = attrs.getAttributeIntValue(ANDROID_NS,
ATTR_DEFAULT_VALUE, 50);
// array.recycle();
}
@Override
protected View onCreateDialogView() {
// TODO Auto-generated method stub
// Get current value from settings
mCurrentValue = getPersistedInt(mDefaultValue);
// Inflate layout
LayoutInflater inflater = (LayoutInflater) getContext()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.seek, null);
// Put minimum and maximum
((TextView) view.findViewById(R.id.text1)).setText(Integer
.toString(mMinValue));
((TextView) view.findViewById(R.id.text2)).setText(Integer
.toString(mMaxValue));
// Setup SeekBar
mSeekBar = (SeekBar) view.findViewById(R.id.seekbar1);
mSeekBar.setMax(mMaxValue - mMinValue);
mSeekBar.setProgress(mCurrentValue - mMinValue);
mSeekBar.setOnSeekBarChangeListener(this);
// Put current value
mValueText = (TextView) view.findViewById(R.id.text3);
mValueText.setText(Integer.toString(mCurrentValue));
return view;
}
@Override
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
// TODO Auto-generated method stub
mCurrentValue = progress + mMinValue;
mValueText.setText(Integer.toString(mCurrentValue));
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
}
这里包含SeekBar的DialogPreference就自定义好了
这时我们需要定义整个Preference的布局文件了dialog_slider.xml
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:example="http://schemas.android.com/apk/res/com.example.dialogpreferencesseekbar" >
<PreferenceCategory android:title="第一层" >
<com.example.dialogpreferencesseekbar.SeekBarPreferences
android:defaultValue="25"
android:dialogTitle="音量进度条"
android:key="seekBarPreference"
android:persistent="true"
android:summary="touch it"
android:title="音量调节"
example:maxValue="100"
example:minValue="0" />
</PreferenceCategory>
<PreferenceCategory android:title="第二层" >
<CheckBoxPreference
android:key="checkbox_preference"
android:summary="this is demo"
android:title="勾选" />
</PreferenceCategory>
</PreferenceScreen>
包含了两层 第一层是我们上边自定义的呼出SeekBar的偏好选项 下边一层是一个CheckBoxPreference
我们PreferenceFragment将dialog_slider.xml包含进去.
MyDialogFragment.java:
package com.example.dialogpreferencesseekbar;
import android.os.Bundle;
import android.preference.PreferenceFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class MyDialogFragment extends PreferenceFragment {
@Override
public void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.layout.dialog_slider);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
return super.onCreateView(inflater, container, savedInstanceState);
}
}
通过FragmentTransaction的replace方法就可以打开我们的自定义的偏好设置了.
效果图: