删除WebView Android中不需要的空白
我已经开始使用WebView开发应用程序。实际上,我正在使用Webview加载图像(我喜欢使用类的内置缩放控件)。我可以成功加载图像,但我可以看到一些令人恼火的空白。我找不到移除它的方法。我的图像大小为750*1000。我在下面附上了我的代码 webview.xml删除WebView Android中不需要的空白,android,image,android-webview,padding,space,Android,Image,Android Webview,Padding,Space,我已经开始使用WebView开发应用程序。实际上,我正在使用Webview加载图像(我喜欢使用类的内置缩放控件)。我可以成功加载图像,但我可以看到一些令人恼火的空白。我找不到移除它的方法。我的图像大小为750*1000。我在下面附上了我的代码 webview.xml <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/andr
<?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>
我真的不知道你指的是哪一个空白(可能图像有填充或其他),但通常如果你在WebView中有任何布局问题,你会尝试通过提供一个合适的(内联的)css样式表来修复它们 默认情况下,webview在主体中有一些边距/填充。如果要删除该填充/边距,请覆盖body标记并添加边距,如:
<body style="margin: 0; padding: 0">
如果您正在谈论的空白在右侧,您可以将其添加到webview中
mWebView.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
如果它位于滚动条所在的右侧,那么应该可以消除空白。使用WebView的
setScrollBarStyle
方法,并将参数设置为视图。通过解除HTML网页的限制,滚动条内部覆盖的填充和边距为10px;您必须在head部分或css文件中设置:
<style type="text/css">
html, body {
width:100%;
height: 100%;
margin: 0px;
padding: 0px;
}
</style>
html,正文{
宽度:100%;
身高:100%;
边际:0px;
填充:0px;
}
这是一个使用html模板进行测试的示例,然后您可以用您想要的任何内容替换“正文”文本。。。在您的例子中,html用于图像。您可以根据需要使用css来设置html的样式。希望这能帮助您更多地了解如何使用本地资产。如果需要更多帮助,请发表评论
一个不可错过的关键行是
将其用作模板:(assets/template.html)
然后在WebView类中:(扩展WebView的类)
将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" />
在我的WebView中,呈现的HTML内容旁边有令人恼火的空白区域(尽管该内容中没有图像)。起初我认为它与上面提到的CSS问题有关。但是通过使用样式表来更改HTML、正文等的背景,它看起来越来越像Web视图周围的填充
user3241873在上面展示了如何调整WebView本身上的填充,但我发现,当Eclipse创建的默认Android应用程序生成布局文件(RelativeLayout“container”而不是“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/values/dimens.xml中查找这些填充值,您将看到它们被设置为16dp。当我更改为0dp时,它删除了难看的空白:)
如果有人用网络视图替换默认的(Hello World)文本视图,则父亲戚yout的填充会像房子里讨厌的亲戚一样粘在一起。我有这个[这是我的活动.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">
我尝试了以上所有的解决方案,但都不太适合我。
这个对我有用。我已经拿到了
这里是我的逻辑代码,当应用程序打开网站时,你必须获得你的网络视图的大小,然后将其设置为高度
这是我的密码
ViewGroup.MarginLayoutParams p = (ViewGroup.MarginLayoutParams) webpage.getLayoutParams();
p.height = webpage.getHeight();
Log.e(" webpage.getHeight()", String.valueOf(webpage.getHeight()));
webpage.setLayoutParams(p);
希望您能将我的代码和答案带到:)适用于任何设备,甚至选项卡:)我的案例,我的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"
/>
您用于测试的手机/标签的尺寸是多少?请尝试将布局更改为“填充父项”。你有什么解决办法吗。我在这里被同一个问题打动了。你能帮帮我吗?是的,这帮了我。必须确保边距和填充均为0。@约翰:您的HTML可能有其他边距/填充。请检查您的HTMLIt是否存在滚动条内部覆盖问题。使用Ben的解决方案解决:webview.setScrollBarStyle(View.SCROLLBARS\u INSIDE\u OVERLAY)
。但是如何将此样式应用于webview,请帮助了解如何将其应用于web视图:String what=“”;loadDataWithBaseURL(“,what,“text/html”,“UTF-8”,”);THx,这确实对我有用。显然,如果您的内容比webview大,因此需要滚动视图,则滚动视图默认不覆盖内容。因此,它需要在WebView的右边缘留出空间来添加图像
,这是WebView
的一个实例,以防有人怀疑它如此简单。。。在一个项目上工作和返工近6个月了,这个问题经常出现,我从来没有想过它会通过像这样的简单命令得到解决。。。非常感谢。Ben You rock::非常感谢,但是如何将这种风格应用于webview呢help@ShirishHerwade . 您需要修改您在webview中调用的html。实际上我不调用任何html,我使用.loadUrl(字符串)来显示图像谢谢!传递给WebView的loadData
的完整字符串可以是:“html,body{width:100%;height:100%;margin:0px;padding:0px;}”+您的html HERE+“”Op谈论的是WebView问题!答案是关于html的。如果我们正在使用somw others网页怎么办?或者无论我们加载什么,我们都可以控制html?有android:padding
属性,不需要单独指定它们。顺便说一句,删除所有填充属性的效果与将它们设置为0dp相同。
<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 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">
ViewGroup.MarginLayoutParams p = (ViewGroup.MarginLayoutParams) webpage.getLayoutParams();
p.height = webpage.getHeight();
Log.e(" webpage.getHeight()", String.valueOf(webpage.getHeight()));
webpage.setLayoutParams(p);
<?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"
/>