【问题标题】:scaling webview to the entire screen in android在android中将webview缩放到整个屏幕
【发布时间】:2012-07-31 14:08:35
【问题描述】:

我正在尝试将 webview 缩放到屏幕大小,但我失败了。我尝试了很多组合,但没有一个对我有用。我在该代码中缺少什么?任何帮助是极大的赞赏。

package com.example.images;

import android.app.Activity; 
import android.os.Bundle; 
import android.webkit.WebView; 

public class MainActivity extends Activity { 

private WebView webView; 
@Override 
public void onCreate(Bundle savedInstanceState) { 
   super.onCreate(savedInstanceState); 
   setContentView(R.layout.main); 
  webView  = (WebView) findViewById(R.id.webview_compontent); 


  String r="<meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0,target-densityDpi=device-dpi\">"; 

        String s="<html><head>"+"<style>body{margin:0; }</style>" +r+"</head>" +"<body>"+
           "<IMG src="+ "\"http://46.20.150.55/member/Resimler/kampanya/195/314/blackmakas1.jpg\">" +
           "<DIV style=\"Z-INDEX: 1; POSITION: absolute; TEXT-ALIGN: center; BACKGROUND-COLOR: white;" +
           "WIDTH: 80px; TOP: 137px; LEFT: 121px\">564524</DIV>"+"</body><html>"; 

   webView.setBackgroundColor(0); 
   webView.setBackgroundResource(R.drawable.android); 
   webView.loadData(s, "text/html","UTF-8");  



  //doesn't work
  /*webView.getSettings().setLoadWithOverviewMode(true); 
   webView.getSettings().setUseWideViewPort(true); 

   try { 
       // load the url 
       webView.loadData(s, "text/html","UTF-8");
   } catch (Exception e) { 
       e.printStackTrace(); 
   } */

   //doesn't work

 /* webView.setInitialScale(1); 
  webView.getSettings().setLoadWithOverviewMode(true); 
  webView.getSettings().setUseWideViewPort(true); 
  webView.getSettings().setJavaScriptEnabled(true); 
  */

  //doesn't work

  /* String p="<html><head>"+"<style>body{margin:0; }</style>" +r+"</head>" +"<body>"+
           "<IMG src="+ "\"http://46.20.150.55/member/Resimler/kampanya/195/314/blackmakas1.jpg\">" +
           "<DIV style=\"Z-INDEX: 1; POSITION: absolute; TEXT-ALIGN: center; BACKGROUND-COLOR: white;" +
           "WIDTH: 80px; TOP: 137px; LEFT: 121px\">564524</DIV>"+"</body><html>"; 

   webView.loadData(p, "text/html","UTF-8");

   webView.getSettings().setJavaScriptEnabled(true); 
   webView.getSettings().setLoadWithOverviewMode(true); 
   webView.getSettings().setUseWideViewPort(true); 
   webView.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY); 
   webView.setScrollbarFadingEnabled(false);   */   

  } } 

这里是 main.xml

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

  >


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

 />


 </LinearLayout>

这里是清单文件

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.images"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="15" />

<application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name=".MainActivity"
        android:label="@string/title_activity_main" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

(已编辑)

我得到的网页是:

这是我得到的页面..我想要的是使广告图像适合整个屏幕。

【问题讨论】:

  • 请显示主布局 xml。顺便说一句,您有可能使用了错误的布局参数(即 wrap_content 而不是 match_parent)。
  • 现在已编辑,我尝试了很多东西,但没有其他工作:(我在哪里犯错了?
  • 好吧,我认为这也应该有效,但请尝试使用相对布局和 match_parent,因为 fill_parent 已过时。
  • 我刚刚按照你说的做了,但是没有用..

标签: android android-webview scaling viewport


【解决方案1】:

问题是您的 webwiew 是全屏的,但 blackmakas 不是。 webView.setBackgroundResource(R.drawable.android); 这一行表明你已经全屏了。问题出在您的 html 中。

【讨论】:

  • 谢谢,我试过了,但现在我得到“应用程序已意外停止”错误..
  • 请将其放在 super.onCreate(savedInstanceState); 之后;
  • 我试过了,还是不行,只是去掉了上面的状态栏,webview的尺寸还是不适合屏幕,还是很小
  • 能否发个截图。然后我们就可以看到了。
  • 我刚刚上传了图片,我想要的是让左边的图片适合整个屏幕。
【解决方案2】:

我认为您需要使用 WEBViewClient 将 url 加载到全屏图像中,因为您面临的问题是您试图在实际设置视图之前加载具有宽度和高度的 webview。

尝试按照这里的教程:http://www.technotalkative.com/android-webviewclient-example/ 和这个:http://www.chrisdanielson.com/tag/webviewclient/

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-10
    • 1970-01-01
    • 2011-12-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多