简体   繁体   English

保存文字大小首选项

[英]Save Text Size Preferences

I have created a custom zoom for my 'TextViews' so the user can zoom in and out. 我已经为“ TextViews”创建了自定义缩放,以便用户可以放大和缩小。 I wanted to hold the position of the zoom so next time they come in the app they wont need to zoom in again. 我想保持缩放的位置,以便下次他们进入应用程序时,无需再次放大。 I was wondering what the best way to do that is? 我想知道这样做的最好方法是什么? Thanks. 谢谢。

I have 2 methods 我有两种方法

get text size: 获取文字大小:

    private void getTextViewSize() {
    boolean gotTextViewsize = false;
    boolean gotDocumentViewsize = false;
    for (int i = 0; i < linearLayout.getChildCount(); i++) {
        if (linearLayout.getChildAt(i) instanceof TextView) {
                (prefs.edit().putFloat("textViewSize",((TextView) linearLayout.getChildAt(i)).getTextSize())).commit();
            gotTextViewsize = true;
        }else if(linearLayout.getChildAt(i) instanceof DocumentView){
            (prefs.edit().putFloat("documentViewSize",((DocumentView) linearLayout.getChildAt(i)).getDocumentLayoutParams().getTextSize())).commit();
            gotDocumentViewsize = true;
        }

        if(gotDocumentViewsize && gotTextViewsize){
            return;
        }
    }
}

This gets the size of the text view which is called after every zoom. 这将获得每次缩放后调用的文本视图的大小。

Next is set text size: 接下来是设置文字大小:

  private void setTextSize() {

    for (int i = 0; i < linearLayout.getChildCount(); i++) {
        if (linearLayout.getChildAt(i) instanceof TextView) {
            if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                ((TextView) linearLayout.getChildAt(i)).setTextSize(prefs.getFloat("textViewSize", 15.0f));
            } else {
                ((TextView) linearLayout.getChildAt(i)).setTextSize(prefs.getFloat("textViewSize", 14.0f));
            }

        }else if (linearLayout.getChildAt(i) instanceof DocumentView){
            ((DocumentView) linearLayout.getChildAt(i)).getDocumentLayoutParams().setTextSize(prefs.getFloat("documentViewSize", 15.0f));
        }
    }
}

This should put the size just how it was however, it makes much much bigger than it was. 这应该使大小保持原样,但是它比原来大得多。 Why is that? 这是为什么?

Use SharedPreferences. 使用SharedPreferences。 They are retained across activity kills and until the user clears the app data. 它们会在活动终止之间保留,直到用户清除应用程序数据为止。

SharedPreferences pref = getApplicationContext().getSharedPreferences("MyPref");
Editor editor = pref.edit();
editor.putInt("zoomLevel",yourZoomLevel);

when retrieving use: 检索使用时:

yourZoomLevel=editor.getInt("zoomLevel",0);

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

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