简体   繁体   English

Android Studio共享首选项设置字体样式

[英]Android Studio Shared Preferences Set Font Style

Currently I'm trying to get the input choice from List Preference which has (Italic, Bold, Underlined) styles in it, but I am not quite sure how to accomplished this specifically. 目前我正在尝试从列表首选项中获取输入选项,其中包含(斜体,粗体,下划线)样式,但我不太确定如何具体完成此操作。

In the past, I've successfully done it for font type, size and colors. 在过去,我已成功完成了字体类型,大小和颜色。

Font Type Example: 字体类型示例:

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

Font Size Example: 字体大小示例:

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

How can I achieve the same idea but with font styles, such as Bold, Italic, Underlined? 我怎样才能实现相同的想法,但使用字体样式,如Bold,Italic,Underlined?

I think you can do this way 我想你可以这样做

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

Basically Typeface.BOLD , Typeface.BOLD_ITALIC , Typeface.ITALIC , Typeface.NORMAL is int as this link said. 基本上Typeface.BOLDTypeface.BOLD_ITALICTypeface.ITALICTypeface.NORMAL就是这个链接所说的int。

Try this. 尝试这个。

Store Font Style 存储字体样式

private void storeFontStyle(int style){

        SharedPreferences sharedPreferences= PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
        SharedPreferences.Editor editor=sharedPreferences.edit();

        editor.putInt("font_face", style);

        editor.apply();

    }

Get font Style 获取字体样式

private int getFontStyle(){

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

    return font_face;
}

Access Font Style 访问字体样式

int font_face=getFontStyle();

    editText.setTypeface(null, font_face);

I can only answer the question for BOLD and ITALIC (I don't think it is possible to specifically underline EditText , but I might be wrong): 我只能回答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 ))

As you can see, the style is just an integer variable inside Typeface that can be read via editText.getTypeface().getStyle() (and then saved to the sharedPreferences ). 如您所见,样式只是Typeface中的一个整数变量,可以通过editText.getTypeface().getStyle()读取(然后保存到sharedPreferences )。

Possible values for the style are: 该样式的可能值为:

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

But in order for this to work you have to make sure that the asset you are creating your font from supports the different font styles (which is normally not the case to my knowledge). 但为了使其工作,您必须确保您创建字体的资产支持不同的字体样式(据我所知通常不是这种情况)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM