图片自适应WebView大小

来源:互联网 发布:app图片编辑软件 编辑:程序博客网 时间:2024/05/03 13:16

mainActivity如下:

package com.cn.testwebview;import android.os.Bundle;import android.view.MotionEvent;import android.view.View;import android.webkit.WebView;import android.app.Activity;/** * Demo描述: * WebView显示图片,但有的图片很小不能铺满整个WebView. * 现解决 */public class MainActivity extends Activity {    private WebView mWebView;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);init();}    private void init(){    mWebView=(WebView) findViewById(R.id.webview);    mWebView.setVerticalScrollBarEnabled(false);    mWebView.setHorizontalScrollBarEnabled(false);    mWebView.getSettings().setJavaScriptEnabled(true);mWebView.getSettings().setUseWideViewPort(true);mWebView.getSettings().setLoadWithOverviewMode(true);String imageUrl="http://csdnimg.cn/www/images/pic_foot_gongshang.png";    String data = "<p><img src=\""+imageUrl+"\" width=\"100%\""+" height=\"100%\""+"  /></p>";    mWebView.loadDataWithBaseURL("", data, "text/html", "UTF-8", "");    //禁止WebView滑动    mWebView.setOnTouchListener(new View.OnTouchListener() {        public boolean onTouch(View v, MotionEvent event) {          return (event.getAction() == MotionEvent.ACTION_MOVE);        }      });    }}


main.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"    android:background="#eee"    >    <ImageView        android:id="@+id/imageView"        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:background="@drawable/ic_launcher" />    <RelativeLayout        android:layout_width="fill_parent"        android:layout_height="match_parent"        android:layout_below="@id/imageView"        android:layout_marginLeft="20dip"        android:layout_marginRight="20dip"       ><WebView    android:id="@+id/webview"    android:layout_width="fill_parent"        android:layout_height="fill_parent"    android:layout_marginBottom="20dp"    />            </RelativeLayout></RelativeLayout>