Android:ListPreference 作为字体大小选择器或访问 ListView 项目的单独视图
Android: ListPreference as font size picker or accessing individual views of ListView items
创建像这样用作 "font size picker" 的 ListPreference 的最佳方法是什么?
有什么方法可以访问该首选项中 ListView 项目的各个视图并设置它们的字体大小?或者我是否必须使用自定义适配器创建自定义 ListPreference?
提前致谢。
已解决。我需要创建自定义 ListPreference
。我找不到如何访问对话框的 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 设置为适当的列表选择单个指示器。
最佳方式(不破坏任何原始 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;
}
创建像这样用作 "font size picker" 的 ListPreference 的最佳方法是什么?
有什么方法可以访问该首选项中 ListView 项目的各个视图并设置它们的字体大小?或者我是否必须使用自定义适配器创建自定义 ListPreference?
提前致谢。
已解决。我需要创建自定义 ListPreference
。我找不到如何访问对话框的 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 设置为适当的列表选择单个指示器。
最佳方式(不破坏任何原始 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;
}