0

在我的应用程序,的Android的WebView内容有时不适合宽度

首先我有一些HTML代码串,然后我把它加载到web视图这样的:

WebView content = (WebView) rootView.findViewById(R.id.content); 

StringBuilder sb = new StringBuilder(); 
     sb.append("<HTML><HEAD><meta name='viewport' content='width=device-width, initial-scale=1.0, maximum-scale=1.0'></HEAD><body>"); 
     sb.append(pageItem.page_content_chi.toString()); 
     sb.append("</body></HTML>"); 

     if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) { 
      content.getSettings().setAllowUniversalAccessFromFileURLs(true); 
      content.getSettings().setAllowFileAccessFromFileURLs(true); 
     } 

     content.setWebChromeClient(new WebChromeClient()); 
     content.setWebViewClient(new WebViewClient() { 
      @Override 
      public boolean shouldOverrideUrlLoading(WebView view, String url) { 
       Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url)); 
       startActivity(intent); 
       return true; 
      } 
     }); 

     content.getSettings().setUseWideViewPort(true); 
     content.getSettings().setJavaScriptEnabled(true); 
     content.getSettings().setLoadWithOverviewMode(true); 
     content.getSettings().setAppCacheMaxSize(1024 * 1024 * 8); 
     String cachePath = ctx.getCacheDir().getAbsolutePath(); 
     content.getSettings().setAppCachePath(cachePath); 
     content.getSettings().setAppCacheEnabled(true); 
     content.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK); 

     content.loadDataWithBaseURL("file:///android_asset/",sb.toString(), "text/html", "utf-8", null); 
     content.setBackgroundColor(0x00000000); 

     content.setOnTouchListener(new View.OnTouchListener() { 
      public boolean onTouch(View v, MotionEvent event) { 
       return (event.getAction() == MotionEvent.ACTION_MOVE); 
      } 
     }); 

     content.setVerticalScrollBarEnabled(false); 
     content.setHorizontalScrollBarEnabled(false); 

布局:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/bg" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" > 

    <ScrollView 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:overScrollMode="never" 
     android:scrollbars="vertical" > 

     <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:orientation="vertical" > 

      <TextView 
       android:id="@+id/title" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_gravity="center_horizontal" 
       android:layout_margin="5dp" 
       android:textSize="25sp" 
       android:textStyle="bold" /> 

      <WebView 
       android:id="@+id/content" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" /> 
     </LinearLayout> 
    </ScrollView> 

</LinearLayout> 

问题是,webview内的内容有时适合宽度,但是,它有时不适合并超出宽度。它随机发生,所以当我点击标签并重新加载webview时,它有时适合视图,有时会超过视图。如何解决这个问题?非常感谢

+0

这可能是因为您使用了宽度=设备宽度,通常网络和移动设备的行为更改。尝试改变它。 –

回答

1

使用的WebView属性为

<WebView 
       android:id="@+id/content" 
       android:layout_width="fill_parent" 
       android:layout_height="fill_parent" /> 

你用这个,你必须看看接下来的事情完成后是,你试图让你的WebView内容的网站响应或者不意味着它是否工作在清晰细腻resolution.if所有屏幕没有,那么你必须做的网页响应于您在网页视图与为例

@media (min:320px , max:568px){ Css Body}

+0

fill_parent现在工作,但它不响应,我如何确定每个移动设备的px大小?谢谢 – user3538235

+0

当你使用CSS文件时,你必须让网站响应我的想法,总共有7个屏幕,或者如果网络专门用于手机,那么只有4个屏幕。如果你已经创建了百分比的网站,那么很容易在一小时内制作4个屏幕。否则我会花2个小时以上px –

+0

您必须检查手机的响应屏幕 –

0

请添加下面的代码我n清单文件:

<supports-screens android:smallScreens="true" 
    android:normalScreens="true" android:largeScreens="true" 
    android:anyDensity="true" />