Android:ListPreference作为字体大小选择器或访问ListView项目的各个视图

时间:2016-01-10 15:20:04

标签: android listview listpreference

创建ListPreference以用作“字体大小选择器”的最佳方法是什么? Font size picker

有什么方法可以在该首选项中访问ListView项目的各个视图并设置它们的字体大小?或者我是否必须使用自定义适配器创建自定义ListPreference?

提前致谢。

2 个答案:

答案 0 :(得分:3)

解决。我需要创建自定义ListPreference。我无法找到如何访问Dialog的ListView的方法。 标题区域保持不变,仅对话框的内容区域已更改。 在onPrepareDialogBuilder(builder)我必须为ListView创建一个自定义适配器,我可以在其中设置不同的字体大小。

public class FontSizeListPreference extends ListPreference {

    private int mClickedDialogEntryIndex;

    public FontSizeListPreference(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public FontSizeListPreference(Context context) {
        super(context);
    }

    @Override
    protected void onPrepareDialogBuilder(AlertDialog.Builder builder) {
        if(getEntries() == null || getEntryValues() == null){
            super.onPrepareDialogBuilder(builder);
            return;
        }

        ArrayAdapter<CharSequence> adapter = new ArrayAdapter<CharSequence>(getContext(), R.layout.preference_font_size_checked_tv, getEntries()) {
            @Override
            public View getView(int position, View convertView, ViewGroup parent) {
                float fontSizePx;
                CheckedTextView view = (CheckedTextView) convertView;
                if(view == null){
                    view = (CheckedTextView) View.inflate(getContext(), R.layout.preference_font_size_checked_tv, null);
                }
                switch (position){
                    case 0:
                        fontSizePx = getContext().getResources().getDimension(R.dimen.font_size_small_medium);
                        break;
                    case 2:
                        fontSizePx = getContext().getResources().getDimension(R.dimen.font_size_large_medium);
                        break;
                    case 1:default:
                        fontSizePx = getContext().getResources().getDimension(R.dimen.font_size_medium_medium);
                }
                view.setText(getEntries()[position]);
                view.setTextSize(TypedValue.COMPLEX_UNIT_PX, fontSizePx);
                return view;
            }
        };

        mClickedDialogEntryIndex = findIndexOfValue(getValue());
        builder.setSingleChoiceItems(adapter, mClickedDialogEntryIndex, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                mClickedDialogEntryIndex = which;
                FontSizeListPreference.this.onClick(dialog, DialogInterface.BUTTON_POSITIVE);
                dialog.dismiss();
            }
        });
        builder.setPositiveButton(null, null);
    }

    @Override
    protected void onDialogClosed(boolean positiveResult) {
        super.onDialogClosed(positiveResult);

        if(positiveResult && mClickedDialogEntryIndex >= 0 && getEntryValues() != null){
            String val = getEntryValues()[mClickedDialogEntryIndex].toString();
            if(callChangeListener(val)){
                setValue(val);
            }
        }
    }
}

/res/layout/preference_font_size_checked_tv.xml 中定义的项目布局:

<?xml version="1.0" encoding="utf-8"?>
<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/text1"
    android:layout_width="match_parent"
    android:layout_height="?android:attr/listPreferredItemHeightSmall"
    android:textAppearance="?android:attr/textAppearanceListItemSmall"
    android:gravity="center_vertical"
    android:checkMark="@null"
    android:drawableLeft="?android:attr/listChoiceIndicatorSingle"
    android:paddingLeft="?android:attr/listPreferredItemPaddingLeft"
    android:paddingRight="?android:attr/listPreferredItemPaddingRight"
    android:height="48dp"/>

我也希望指标位于左侧。这就是为什么checkMark被设置为null并且drawableLeft被设置为适当的列表选择单个指示符。

答案 1 :(得分:0)

最佳方式(不破坏任何原始的ListPreference实现):

@Override
protected void onPrepareDialogBuilder(Builder builder) {            

    ArrayAdapter<CharSequence> adapter = new ArrayAdapter<CharSequence>(getContext(), R.layout.preference_font_size_checked_tv, getEntries()) {
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            float fontSizePx;
            CheckedTextView view = (CheckedTextView) convertView;
            if(view == null){
                view = (CheckedTextView) View.inflate(getContext(), R.layout.preference_font_size_checked_tv, null);
            }
            switch (position){
                case 0:
                    fontSizePx = getContext().getResources().getDimension(R.dimen.font_size_small_medium);
                    break;
                case 2:
                    fontSizePx = getContext().getResources().getDimension(R.dimen.font_size_large_medium);
                    break;
                case 1:default:
                    fontSizePx = getContext().getResources().getDimension(R.dimen.font_size_medium_medium);
            }
            view.setText(getEntries()[position]);
            view.setTextSize(TypedValue.COMPLEX_UNIT_PX, fontSizePx);
            return view;
        }
    };
    builder.setAdapter(adapter, this);
    super.onPrepareDialogBuilder(builder);
}

@Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
    preference.setSummary(getEntry());
    return true;
}