的WebView:如何preserve跨会话用户的变焦设置?变焦、用户、WebView、preserve

2023-09-12 08:00:14 作者:眼累了海茫了心丢了城

我的应用程序使用的WebView,并且用户有时会调整缩放比例,以使文字较大。然而,当活动被关闭缩放级别设置被丢失,另一个开始。

My app uses a WebView, and users sometimes adjust the zoom level to make the text larger. However the zoom level setting is lost when the Activity is closed and another one started.

我不明白怎么去和的WebView编程设置缩放级别,任何人都可以提出一个方法能够做到这一点?

I can't see how to get and set the zoom level programatically on WebView, can anyone suggest a way this could be done?

推荐答案

它我豪尔赫从了Grupo Reforma的,这是我的实现使用共享preferences ..

its me Jorge from Grupo Reforma, this is my implementation using SharedPreferences..

static final  String PREFS_Zoom = "PREFS_Zoom";

 private String zoomlevel;
 private int Default_zoomlevel=100;

     @Override
     public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.main);

      mWebView = (WebView) findViewById(R.id.webview);        

      GetZoom();
      mWebView.setInitialScale(Default_zoomlevel);

      FrameLayout mContentView = (FrameLayout) getWindow().getDecorView().findViewById(android.R.id.content);
      View zoom = mWebView.getZoomControls();
      mContentView.addView(zoom, ZOOM_PARAMS);
      zoom.setVisibility(View.VISIBLE);

      WebSettings webSettings = mWebView.getSettings();
      webSettings.setSavePassword(false);
      webSettings.setSaveFormData(false);
      webSettings.setJavaScriptEnabled(true);
      webSettings.setSupportZoom(true);

      mWebView.loadUrl("http://www.elnorte.com");
    }



     private void GetZoom(){
      try{
       SharedPreferences settings = getSharedPreferences(PREFS_Zoom,0);
          zoomlevel = settings.getString("zoom_level","");     
       if (zoomlevel.length() >0)   
        Default_zoomlevel = Integer.parseInt(zoomlevel);               
       else
       Default_zoomlevel =100;
      }catch(Exception ex){
       Log.e("******ZOOM ! ", "Exception GetZoom()  ::"+ex.getMessage());          
      }
     }


     private void SaveZoom(){
      try{
       SharedPreferences settings = getSharedPreferences(PREFS_Zoom,0);            
       SharedPreferences.Editor editor = settings.edit();    
       Default_zoomlevel = (int) (mWebView.getScale() *100);
       editor.putString("zoom_level",""+ Default_zoomlevel);   
       editor.commit();
      }catch(Exception ex){
       Log.e("******ZOOM ! ", "Exception SaveZoom()  ::"+ex.getMessage());    
      }
     }


     public void finish() {  
      SaveZoom();
          super.finish();     
     }

我希望这有助于

I hope this help