【问题标题】:Android Studio Shared Preferences Set Font StyleAndroid Studio 共享首选项设置字体样式
【发布时间】:2017-07-12 05:14:56
【问题描述】:

目前我正在尝试从列表首选项中获取输入选项,其中包含(斜体、粗体、下划线)样式,但我不太确定如何具体完成此操作。

在过去,我已经成功地完成了字体类型、大小和颜色。

字体类型示例:

         SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
    String s = sharedPreferences.getString("font_list", "gnuolane rg.ttf");
    Typeface face = Typeface.createFromAsset(getAssets(), "fonts/" + s);
    editText.setTypeface(face);

字体大小示例:

         String s2 = sharedPreferences.getString("font_size", "8");
    editText.setTextSize(Float.parseFloat(s2))

如何使用粗体、斜体、下划线等字体样式实现相同的想法?

【问题讨论】:

    标签: android android-studio fonts mobile-development


    【解决方案1】:

    我认为你可以这样做

    SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
    int font_face = sharedPreferences.getInt("font_face", Typeface.NORMAL);
    editText.setTypeface(Integer.toString(font_face));
    

    基本上Typeface.BOLDTypeface.BOLD_ITALICTypeface.ITALICTypeface.NORMAL 是 int 如this link 所说。

    【讨论】:

    • 出于某种原因抱怨“Typeface.NORMAL”这一行
    • 抱歉,我现在将getString 固定为getInt
    【解决方案2】:

    试试这个。

    商店字体样式

    private void storeFontStyle(int style){
    
            SharedPreferences sharedPreferences= PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
            SharedPreferences.Editor editor=sharedPreferences.edit();
    
            editor.putInt("font_face", style);
    
            editor.apply();
    
        }
    

    获取字体样式

    private int getFontStyle(){
    
        SharedPreferences sharedPreferences= PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
        int font_face = sharedPreferences.getInt("font_face", Typeface.NORMAL);
    
        return font_face;
    }
    

    访问字体样式

    int font_face=getFontStyle();
    
        editText.setTypeface(null, font_face);
    

    【讨论】:

    • 它抱怨“Typeface.Normal”和“Integer.toString(font_face));
    • 因为我猜共享偏好不能将 int 作为第二个参数
    • 你检查过这个吗?
    • 是的。它使应用程序崩溃
    【解决方案3】:

    我只能回答 BOLDITALIC 的问题(我认为不可能专门在 EditText 下划线,但我可能是错的):

    String fontSize = sharedPreferences.getString("font_size", "8");
    String style= sharedPreferences.getInt("font_style", TypeFace.NORMAL);
    String font = sharedPreferences.getString("font_list", "gnuolane rg.ttf");
    Typeface face = Typeface.create(Typeface.createFromAsset(getAssets(), "fonts/" + s),style);
    editText.setTextSize(Float.parseFloat(fontSize ))
    

    如您所见,样式只是 Typeface 中的一个 integer 变量,可以通过 editText.getTypeface().getStyle() 读取(然后保存到 sharedPreferences)。

    样式的可能值是:

    Typeface.NORMAL
    Typeface.BOLD
    Typeface.ITALIC
    Typeface.BOLD_ITALIC
    

    但为了使其正常工作,您必须确保创建字体的资产支持不同的字体样式(据我所知,这通常不是这种情况)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-08
      • 2014-10-26
      • 2011-04-08
      • 1970-01-01
      相关资源
      最近更新 更多