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

标签 android listview listpreference

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

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

提前致谢。

最佳答案

已解决。我需要创建自定义 ListPreference。我找不到如何访问对话框的 ListView 的方法。 标题区域保持不变,只有对话框的内容区域发生了变化。 在 onPrepareDialogBu​​ilder(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 设置为适当的列表选择单个指示器。

关于Android:ListPreference 作为字体大小选择器或访问 ListView 项目的各个 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34707077/

相关文章:

java - 简单 ListPreference 不起作用。有任何想法吗?

java - 如何获取 XML 中定义的 ListPreference 的默认值?

Java/Android 更改由对象 ArrayList 填充的 ListView 中的数据

java - dalvik Java 如何使用反射调用父类(super class)方法?

模块之间的 Android Kotlin Compose 编译器版本不匹配

java - LinearLayout 在小型设备中没有响应

listview - 通过 ListView / GridView 显示记录

java - 在我的 ArrayList 上保存项目(添加不断替换第一个)

android - ListPreference 字体大小

android - TimePickerFragment 不工作