标签:android preference spinner widget 布局
因客户需求SpinnerPreference,网上各种搜索不到,无奈只能重写组件,现将过程分享大家。
public MySpinnerPreference(Context context) { super(context); } public MySpinnerPreference(Context context, AttributeSet attrs) { super(context, attrs); } public MySpinnerPreference(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); }
<?xml version="1.0" encoding="utf-8"?> <!-- Layout of a header item in PreferenceActivity. --> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="?android:attr/activatedBackgroundIndicator" android:gravity="center_vertical" android:minHeight="48dp" android:paddingEnd="?android:attr/scrollbarSize" android:paddingStart="@dimen/mypreference_margin_start" > <RelativeLayout android:layout_width="0dp" android:layout_height="wrap_content" android:layout_marginBottom="6dip" android:layout_marginEnd="6dip" android:layout_marginStart="2dip" android:layout_marginTop="6dip" android:layout_weight="1" android:gravity="center_vertical" > <TextView android:id="@+android:id/title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:ellipsize="marquee" android:fadingEdge="horizontal" android:singleLine="true" android:textAppearance="?android:attr/textAppearanceMedium" /> <TextView android:id="@+android:id/summary" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignStart="@android:id/title" android:layout_below="@android:id/title" android:ellipsize="end" android:maxLines="2" android:textAppearance="?android:attr/textAppearanceSmall" /> </RelativeLayout> <!-- Preference should place its actual preference widget here. --> <Spinner android:id="@+id/spinner1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:padding="8dip" /> </LinearLayout>
@Override protected View onCreateView(ViewGroup parent) { // TODO Auto-generated method stub return super.onCreateView(parent); } @Override protected void onBindView(View view) { super.onBindView(view); mSpinner = (Spinner) view.findViewById(R.id.spinner1); String[] arraystr = view.getResources().getStringArray(R.array.itemspinner_values); mAdapter = new ArrayAdapter<String>(view.getContext(), android.R.layout.simple_spinner_dropdown_item, arraystr); // 也可一自己定义适配器的样式 // mAdapter = new ArrayAdapter<String>(view.getContext(), R.layout.preference_categoary, mMeausStr); mSpinner.setAdapter(mAdapter); mSpinner.setOnItemSelectedListener(this); }
@Override public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { if (callChangeListener(position)) { setValue(position); } } @Override public void onNothingSelected(AdapterView<?> parent) { }
設置我們的改動
public void setValue(int value) { // Always persist/notify the first time. final boolean changed = !TextUtils.equals(Integer.toString(mValue), Integer.toString(value)); if (changed || !mValueSet) { mValue = value; mValueSet = true; persistInt(value); if (changed) { notifyChanged(); } } } @Override public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { mPos = position; if (callChangeListener(position)) { setValue(position); } }
可以持久化多种基本数据类型
@Override protected void onSetInitialValue(boolean restoreValue, Object defaultValue) { setValue(restoreValue ? getPersistedInt(mValue) : (Integer) defaultValue); }
自定义 Android Preference——SpinnerPreference的私人定制,布布扣,bubuko.com
自定义 Android Preference——SpinnerPreference的私人定制
标签:android preference spinner widget 布局
原文地址:http://blog.csdn.net/wang9258/article/details/38144813