2012-01-01 40 views
33

我是Android新手。我已經開始使用WebView開發一個應用程序。其實我用Webview加載圖像(我喜歡使用類的內置縮放控件)。我可以成功加載圖像,但我可以看到一些惱人的白色空間。我找不到將其刪除的方法。我的圖像尺寸爲750 * 1000。我已附上我的代碼如下,任何幫助將不勝感激!在WebView中刪除不需要的空白Android

webview.xml

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

ImageViewer.java

package com.android.test; 

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

public class ImageViewer extends Activity { 

    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.webview); 

     String loadUrl = "file:///android_res/drawable/splash_001.jpg"; 

     WebView image = (WebView) findViewById(R.id.webView); 
     image.clearView(); 
     image.clearCache(true); 
     image.getSettings().setBuiltInZoomControls(true); 
     image.getSettings().setSupportZoom(true); 
     image.getSettings().setLoadWithOverviewMode(true); 
     image.getSettings().setUseWideViewPort(true); 
     image.loadUrl(loadUrl); 
    } 

} 

請幫助我!

+0

您用於測試的手機/選項卡的尺寸是多少? – 2012-01-01 17:17:32

+0

嘗試將您的layout_xx更改爲「填充父項」 – Ghost 2012-01-02 05:15:11

+1

嗨。你有沒有解決這個問題。我在這裏遇到了同樣的問題。你能幫我嗎 – 2012-01-05 06:21:42

回答

0

我真的不知道你指的是哪個空白(可能是圖像有填充等),但通常如果你在WebView中有任何佈局問題,你可以嘗試通過提供適當的(內聯) CSS樣式表。

47

默認情況下webview在body中有一些margin/padding。如果你想刪除填充/保證金然後覆蓋身體標籤並添加保證金,如:

<body style="margin: 0; padding: 0"> 
+0

是的,那是爲我做的。必須確保邊距和填充都爲0. – Richard 2012-02-10 20:05:10

+1

對我不起作用 – 2014-07-03 17:11:21

+0

@John:您的HTML可能有其他一些邊距/填充。請看看你的HTML – Sandy 2014-07-04 05:37:00

20

如果你談論的是白色空間的右側,你可以添加這對web視圖。

mWebView.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY); 

這應該使空白消失,只要它位於滾動條的右側即可。

+0

THx,這對我有用。顯然,如果你的內容比你的webview更大,所以需要一個滾動視圖,默認情況下滾動視圖不會覆蓋內容。所以它需要它的空間在webview的右邊緣 – 2013-04-30 09:09:31

+0

@謝謝。幾個月來,這個問題一直困擾着我。 – hannebaumsaway 2013-09-04 17:49:49

+1

只要添加'image'就是'WebView'的一個實例,以防萬一有人想知道 – donturner 2013-12-19 21:42:50

3

使用WebView的setScrollBarStyle方法並提供參數View.SCROLLBARS_INSIDE_OVERLAY

14

由Defualt HTML網頁有一個填充和10px的邊距;你必須在你的頭部或者css文件中設置:

<style type="text/css"> 
html, body { 
width:100%; 
height: 100%; 
margin: 0px; 
padding: 0px; 
} 
</style> 

這對我來說算得了。

+0

但如何將這種風格應用於webview,請幫忙 – 2015-01-29 12:03:24

+1

@ShirishHerwade。你需要修改你調用webview的html。 – 2015-01-30 07:38:04

+0

其實我不會調用任何html,我用.loadUrl(String)來顯示圖片 – 2015-01-30 10:18:49

1

這是一個例子,你可以測試使用html模板,然後你可以用你想要的任何東西替換「body」文本......在你的情況下,圖像的html。您可以根據需要使用CSS來設置html的樣式。希望這有助於您更好地瞭解如何使用本地資產。如果您需要更多幫助,請發表評論。

一個鍵行不會錯過是<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0,target-densityDpi=device-dpi" />

以此爲模板:(資產/ template.html)

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> 
<head> 
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0,target-densityDpi=device-dpi" /> 
    <link rel="stylesheet" type="text/css" href="style.css" /> 
</head> 
<body> 
[CONTENT] 
</body> 
</html> 

使用CSS是這樣的:(資產/ style.css文件)

@font-face { 
    font-family: Roboto; 
    src: url(file:///android_asset/fonts/Roboto.ttf); 
} 

html, div { 
    margin: 0; 
    padding: 0; 
} 

body { 
    font-family: Roboto; 
    font-size: 1.0em; 
    color: rgb(61,61,61); 
    text-align: left; 
    position:relative; 
    padding: 40px; 
    background-color: #eeeeee; 
} 

然後在您的WebView類:(你的類,它擴展的WebView)

public void setText(CharSequence text, int color){ 

     try { 

      InputStream is = getResources().getAssets().open("article_view.html"); 
      int size = is.available(); 
      byte[] buffer = new byte[size]; 
      is.read(buffer); 
      is.close(); 

      loadDataWithBaseURL("file:///android_asset/", 
       new String(buffer).replace("[CONTENT]", text.toString()), 
       "text/html", "UTF-8", null); 

     } catch (IOException e) { 
      e.printStackTrace(); 
     } 

} 
0

在你的webview中設置填充。XML來0dp

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" > 
<WebView 
    android:id="@+id/webView" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:paddingBottom="0dp" 
    android:paddingLeft="0dp" 
    android:paddingRight="0dp" 
    android:paddingTop="0dp" /> 

+0

有'android:padding'屬性,不需要單獨指定它們全部。而且,順便說一句,刪除所有填充屬性將具有相同的效果,因爲它們設置爲0dp。 – ccpizza 2015-12-05 10:12:39

7

我有刺激性的白色空間在我的WebView呈現的HTML內容(該內容沒有圖像雖然)接壤。起初我認爲它與上面提到的CSS問題有關。但通過使用樣式表來改變HTML,BODY等的背景,它看起來越來越像WebView的填充。

user3241873上面顯示瞭如何調整WebView本身的填充,但是我發現當Eclipse創建的默認Android應用程序生成佈局文件(RelativeLayout「容器」而不是「LinearLayout」)時,它添加了這些屬性到的RelativeLayout:

android:paddingBottom="@dimen/activity_vertical_margin" 
android:paddingLeft="@dimen/activity_horizontal_margin" 
android:paddingRight="@dimen/activity_horizontal_margin" 
android:paddingTop="@dimen/activity_vertical_margin" 

如果你看看在res /價值/ dimens.xml這些填充值,你會發現它們設置爲16DP。當我改爲0dp時,它刪除了難看的空白區域:)

如果某人用WebView替換了默認(Hello World)TextView,則父親RelativeLayout的填充會像家中的討厭親戚一樣粘滯。

+0

這幫了我。謝謝! – 2015-01-06 06:57:56

1

我有這個[這是我activity.xml]

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" 
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin" 
android:paddingRight="@dimen/activity_horizontal_margin" 
android:paddingTop="@dimen/activity_vertical_margin" 
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity"> 

我只是刪除在RelativeLayout的

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" 
android:layout_height="match_parent" 
tools:context=".MainActivity"> 

我嘗試了上述所有解決方案的填充,但他們沒有對我來說很好。 這一個爲我工作。

-1

我已經知道了。

我在這裏的邏輯代碼,當應用程序打開該網站,你必須讓你的網頁流量的大小,然後將其放置在高度

這裏我的代碼

ViewGroup.MarginLayoutParams p = (ViewGroup.MarginLayoutParams) webpage.getLayoutParams(); 
     p.height = webpage.getHeight(); 
     Log.e(" webpage.getHeight()", String.valueOf(webpage.getHeight())); 
     webpage.setLayoutParams(p); 

希望你們對我的代碼和我回答:)在任何設備作品甚至標籤也:)

-1

我的情況下,我的webview的頂部有大填充。所以,設置scrollbarSize =「0dp」。在AS 2.2.3中

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

<WebView 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:id="@+id/webView" 
android:scrollbarSize="0dp" 
/>