[英]Android webview content sometimes not fit the width
在我的申請中,
首先,我將一些html代碼作為string,然后像這樣將其加載到webview中:
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中的內容有時適合寬度,但是有時不適合並且超過寬度。 它是隨機發生的,因此,當我單擊選項卡並重新加載Web視圖時,它有時適合視圖,有時超出視圖。 如何解決這個問題? 非常感謝
將WebView屬性用作
<WebView
android:id="@+id/content"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
完成此操作后,您接下來要看到的是您試圖在Web視圖中獲取內容的網站是否具有響應能力,這意味着該網站是否適用於所有屏幕且分辨率清晰。那么您必須使網頁響應Webview中的示例,例如
@media (min:320px , max:568px ){ Css Body}
請在清單文件中添加以下代碼:
<supports-screens android:smallScreens="true"
android:normalScreens="true" android:largeScreens="true"
android:anyDensity="true" />
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.