[英]How to resize the webview According to the size of device Screen in android studio
[英]How to resize the webview According to the size of device Screen (Or Width) in android
我有一個帶有webView的Custom ListView。 這是XML代碼
reader_layout.xml
<ListView
android:id="@+id/listReader"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:layout_below="@+id/readerHeader"
android:divider="@android:color/background_dark"
android:dividerHeight="5dp" >
</ListView>
reader_list_webview.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<WebView
android:id="@+id/webView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:clickable="false"
android:focusable="false"
android:focusableInTouchMode="false"
android:scrollbars="none" />
</RelativeLayout>
我使用AsyncTask和BaseAdapter用WebView填充listView,並且我在每個webView中加載了不同的URL。 這是java代碼。
public class ImageAdapter extends BaseAdapter { public ImageAdapter(Context c) { context = c; } // ---returns the number of images--- public int getCount() { return URLs.size(); } public ImageAdapter(Context ctx, Activity act) { inflater = (LayoutInflater) act .getSystemService(Context.LAYOUT_INFLATER_SERVICE); } // ---returns the ID of an item--- public Object getItem(int position) { return position; } public long getItemId(int position) { return position; } // ---returns an ImageView view--- public View getView(final int position, View convertView, ViewGroup parent) { // ImageView bmImage; final ViewHolder holder; View vi = convertView; if (convertView == null) { vi = inflater.inflate(R.layout.book_reader_list_style, parent, false); holder = new ViewHolder(); holder.webView = (WebView) vi.findViewById(R.id.webView1); WebSettings webSettings = holder.webView.getSettings(); webSettings.setCacheMode(WebSettings.LOAD_DEFAULT); holder.webView.setVerticalScrollBarEnabled(false); holder.webView.setHorizontalScrollBarEnabled(false); holder.webView.setVisibility(View.VISIBLE); webSettings.setLoadWithOverviewMode(true); webSettings.setUseWideViewPort(false); holder.webView.setInitialScale(scaleWebView); vi.setTag(holder); } else { holder = (ViewHolder) vi.getTag(); } holder.webView.loadUrl((URLs.get(position))); return vi; }
}
class ViewHolder {WebView webView; }
一切都很好。 但問題是一些URL變得比webview大小更寬,因此它們不適合設備屏幕寬度,而一些URL加載小頁面因此我們需要設置webview的縮放。
我試過這個
android:layout_width="match_parent"
android:layout_height="match_parent"
和這個
myWebview.setLoadWithOverviewMode(true);
myWebview.setUseWideViewPort(true);
這也是
holder.webView.setInitialScale(percent);
但它無法解決我的要求。
如果我設置setLoadWithOverviewMode(true/false);
真的我的應用程序崩潰,這里是Logcat
01-08 16:16:56.600: E/AndroidRuntime(8666): java.lang.IllegalArgumentException: bitmap size exceeds 32bits
你可以試試這個..
/**This is for to set wide */
mWebView.getSettings().setUseWideViewPort(true);
mWebView.getSettings().setLoadWithOverviewMode(true);
最后,我自己解決了我的解決方案。 這里是
holder.webView.loadDataWithBaseURL(null,"<!DOCTYPE html><html><body style = \"text-align:center\"><img style=\"border-style:dotted;border-width:5px;border-color:black;width:99%;\" src= " + URLs.get(position)+ " alt=\"page Not Found\"></body></html>","text/html", "UTF-8", null)
它將完全適合webView的標簽填充屏幕。 無論你的手機是230X480還是你有800X1200的任何Android平板電腦。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.