2016-09-14 76 views
0

我正在动态添加webview的线性布局。我的问题是,当方向改变文字大小时也会发生变化。例如,当应用处于横向模式时,文字大小是大的,而纵向模式是中等大小。我希望在方向更改时保持文本大小中等。 这就是我想实现的代码: -如何防止webview在方向更改时更改文本大小?

private void handle_webcontent(String contentType, final String content) { 
    String html_open = "<html><head><style>@font-face {font-family: 'verdana';src: url('file://"+ this.getFilesDir().getAbsolutePath()+ "/verdana.ttf');}body {font-family: 'verdana';}</style><title></title></head><body>"; 
    String html_close = "</body></html>"; 
    final WebView webView1; 
    webView1 = new WebView(this); 
    LinearLayout.LayoutParams layoutParam = new LinearLayout.LayoutParams(
      LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT); 
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { 
     layoutParam.setMargins(10,0,10,10); 
     webView1.setLayoutParams(new LinearLayout.LayoutParams(layoutParam)); 

    } 
    web_linearLayout.addView(webView1); 
    switch (contentType) { 
     case "text": 
      if (content.equals("NA")) { 
       Log.d("CONTENT", "NOTHING TO DISPLAY"); 

      } else { 
       Document doc = Jsoup.parse(html_open); 
       doc.append(content); 
       doc.append(html_close); 
       webView1.getSettings().setJavaScriptEnabled(true); 
       webView1.setInitialScale(getScale()); 
       webView1.getSettings().setAllowFileAccess(true); 
       webView1.getSettings().setLoadsImagesAutomatically(true); 
       webView1.setWebViewClient(
         new WebViewClient() { 

          @Override 
          public void onPageFinished(WebView view, String url) { 
           String javascriptCode = "javascript:"; 
           javascriptCode+="var elements=document.querySelectorAll(\"code\");"; 
           javascriptCode+="var parent;"; 
           javascriptCode+="var container;"; 
           javascriptCode+="for(i in elements){"; 
           javascriptCode+="container = document.createElement('div');"; 
           javascriptCode+="parent = elements[i].parentElement;"; 
           javascriptCode+="parent.replaceChild(container, elements[i]);"; 
           javascriptCode+="container.appendChild(elements[i]);"; 
           javascriptCode+="container.setAttribute(\"style\",\" white-space: nowrap; background-color: #EEEEEE; padding-top: 4px; padding-right: 4px; padding-bottom: 4px; padding-left: 4px;\");}"; 
           webView1.loadUrl(javascriptCode); 
          } 
         } 
       ); 
       webView1.loadDataWithBaseURL("", String.valueOf(doc), "text/html", "UTF-8", ""); 
       Log.d("CODE CONTENT",String.valueOf(doc)); 


      } 

      break; 
     case "image": 
      if (content.equals("NA")) { 
       Log.d("CONTENT", "NOTHING TO DISPLAY"); 
      } else { 
       String image = image_url + content; 
       downloadImages(image); 
      } 
      break; 
     case "button": 
      if (content.equals("NA")) { 
       Log.d("CONTENT", "NOTHING TO DISPLAY"); 

      } else { 
       Button button = (Button) getLayoutInflater().inflate(R.layout.button_template, null); 
       LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT); 
       layoutParams.gravity = Gravity.CENTER_HORIZONTAL; 
       button.setLayoutParams(layoutParams); 
       web_linearLayout.addView(button); 

       final WebView webView; 
       webView = new WebView(this); 
       webView.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT)); 
       web_linearLayout.addView(webView); 


       button.setOnClickListener(new View.OnClickListener() { 
        @Override 
        public void onClick(View view) { 
         String html_open = "<html><head><style>@font-face {font-family: 'verdana';src: url('file://"+ DisplayContent.this.getFilesDir().getAbsolutePath()+ "/verdana.ttf');}body {font-family: 'verdana';}</style><title></title></head><body>"; 
         String html_close = "</body></html>"; 
         Document doc = Jsoup.parse(html_open); 
         doc.append(content); 
         doc.append(html_close); 
         webView.getSettings().setJavaScriptEnabled(true); 
         webView.getSettings().setAllowFileAccess(true); 
         webView.getSettings().setLoadsImagesAutomatically(true); 
         webView.loadDataWithBaseURL("", String.valueOf(doc), "text/html", "utf-8", ""); 

        } 
       }); 
      } 

      break; 
     default: 
    } 

} 

任何帮助或建议是appreciated.Thank你。

回答

1

在你的活动提这个

android:configChanges="keyboardHidden|orientation" 

android:configChanges="orientation|screenSize" 
+0

我想这个机器人:configChanges =“方向|屏幕尺寸”,但它并没有为我 – AndroidNewBee

+0

工作,请分享你的清单文件内容如果你发现方向变化有任何错误,记录下来并记录下来 –

+0

嘿,这实际上工作。谢谢戈卡莱先生。 – AndroidNewBee