android - 当我在android中单击ListPreference时,如何设置对话框中项目的字体大小?

标签 android

我在PreferenceScreen中有一个ListPreference控件,我希望更改ListPreference对话框中项目的字体大小,我尝试在ListPreference中添加样式,但似乎不起作用,我该怎么办 做 ?谢谢!

顺便说一句,我读过这篇文章custom row in a listPreference? ,我认为这太复杂了,我只需要更改 ListPreference 对话框中列出的项目的字体大小即可。

<ListPreference
style="@style/Text.ListPreference"/>

<style name="Text.ListPreference">
    <item name="android:textSize">15sp</item>
</style>





<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:robobunny="http://robobunny.com"
    android:key="AppPreference"
    android:summary="@string/PreferenceSummary"
    android:title="@string/Preference" >

     <PreferenceCategory android:title="@string/PreferenceCallCategory"
        android:layout="@layout/preference_category_layout">         

       <ListPreference
          android:key="CallOption"
          android:defaultValue="AllNumber"       
          android:entries="@array/CallAndSMSOption"
          android:entryValues="@array/CallAndSMSOption_values"       
          android:title="@string/CallOptionTitle"
          android:summary="@string/CallOptionSummary" 
          style="@style/myTextLarge"    
          android:layout="@layout/preference_layout"  
        /> 

      </PreferenceCategory> 


</PreferenceScreen>

最佳答案

import android.app.AlertDialog.Builder;
import android.content.Context;
import android.content.DialogInterface;
import android.content.SharedPreferences.Editor;
import android.graphics.Typeface;
import android.preference.DialogPreference;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckedTextView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class FontPreference extends DialogPreference implements DialogInterface.OnClickListener {

    // Keeps the font file paths and names in separate arrays
    private List<String> m_fontPaths;
    private List<String> m_fontNames;

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

    @Override
    protected void onPrepareDialogBuilder(Builder builder) {
        super.onPrepareDialogBuilder(builder);
        // Get the fonts on the device
        HashMap<String, String> fonts = FontManager.enumerateFonts();
        m_fontPaths = new ArrayList<String>();
        m_fontNames = new ArrayList<String>();
        // Get the current value to find the checked item
        String selectedFontPath = getSharedPreferences().getString(getKey(), "");
        int idx = 0, checked_item = 0;
        for (String path : fonts.keySet()) {
            if (path.equals(selectedFontPath))
                checked_item = idx;
            m_fontPaths.add(path);
            m_fontNames.add(fonts.get(path));
            idx++;
        }
        // Create out adapter
        // If you're building for API 11 and up, you can pass builder.getContext
        // instead of current context
        FontAdapter adapter = new FontAdapter();
        builder.setSingleChoiceItems(adapter, checked_item, this);
        // The typical interaction for list-based dialogs is to have click-on-an-item dismiss the dialog
        builder.setPositiveButton(null, null);
    }

    public void onClick(DialogInterface dialog, int which) {
        if (which >= 0 && which < m_fontPaths.size()) {
            String selectedFontPath = m_fontPaths.get(which);
            Editor editor = getSharedPreferences().edit();
            editor.putString(getKey(), selectedFontPath);
            editor.commit();
            dialog.dismiss();
        }
    }


    // Font adaptor responsible for redrawing the item TextView with the appropriate font.
    // We use BaseAdapter since we need both arrays, and the effort is quite small.
    public class FontAdapter extends BaseAdapter {
        @Override
        public int getCount() {
            return m_fontNames.size();
        }

        @Override
        public Object getItem(int position) {
            return m_fontNames.get(position);
        }

        @Override
        public long getItemId(int position) {
            // We use the position as ID
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View view = convertView;
            // This function may be called in two cases: a new view needs to be created,
            // or an existing view needs to be reused
            if (view == null) {
                // Since we're using the system list for the layout, use the system inflater
                final LayoutInflater inflater = (LayoutInflater)
                        getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                // And inflate the view android.R.layout.select_dialog_singlechoice
                // Why? See com.android.internal.app.AlertController method createListView()
                view = inflater.inflate(android.R.layout.select_dialog_singlechoice, parent, false);
            }
            if (view != null) {
                // Find the text view from our interface
                CheckedTextView tv = (CheckedTextView) view.findViewById(android.R.id.text1);
                // Replace the string with the current font name using our typeface
                Typeface tface = Typeface.createFromFile(m_fontPaths.get(position));
                tv.setTypeface(tface);
                // If you want to make the selected item having different foreground or background color,
                // be aware of themes. In some of them your foreground color may be the background color.
                // So we don't mess with anything here and just add the extra stars to have the selected
                // font to stand out.
                tv.setText(m_fontNames.get(position));
            }
            return view;
        }
    }
}

结束参见this answer

关于android - 当我在android中单击ListPreference时,如何设置对话框中项目的字体大小?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25130338/

相关文章:

android - Asynctask 不会更新数组

java - 将图片从 URI 复制到文件时图片旋转了 90 度

java - 如何以编程方式更改android中imagebutton的大小

java - 删除当前 fragment ,并替换为新 fragment

android - Android (ARCore) 阻塞上的 PixelBuffer 对象和 glReadPixel

android - Drawable 占用的内存是否比 Bitmap 少?

android - 如何通过android模拟器找到本地服务器?

android-studio 找不到在类里面使用的 aidl 接口(interface)

java - 如何设置多列表代号一个的样式?

android - 如何在回收站 View 中设置 On Item Click 监听器并将一个 Activity 中来自 JSON 的数据发送到另一个 Activity ?