如何使WebView高度尺寸与屏幕高度相适应?
如果我的 WebView 太大,我没有问题,即使内容太大我可以滚动 WebView 的内容,WebView 高度也适合屏幕高度。 但是如果WebView的内容太少,WebView的高度就会不适合屏幕。
这是我的布局:
<?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="wrap_content" >
<ScrollView android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:fitsSystemWindows="true">
<WebView android:id="@+id/webview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:fitsSystemWindows="true" />
</ScrollView>
<LinearLayout android:id="@+id/media_player"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:visibility="visible">
<Button android:textStyle="bold"
android:id="@+id/ButtonPlayStop"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@android:drawable/ic_media_play" />
<SeekBar android:id="@+id/SeekBar"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:layout_below="@id/ButtonPlayStop" />
</LinearLayout>
这是屏幕截图:
任何人都可以帮助我解决这个问题吗?
If my WebView is so large, I have no problem and WebView height fit the screen height even when the content is so bigger I can scroll the content of WebView.
But if the content of WebView is little, the WebView height doesn't fit the screen.
This is my layout:
<?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="wrap_content" >
<ScrollView android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:fitsSystemWindows="true">
<WebView android:id="@+id/webview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:fitsSystemWindows="true" />
</ScrollView>
<LinearLayout android:id="@+id/media_player"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:visibility="visible">
<Button android:textStyle="bold"
android:id="@+id/ButtonPlayStop"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@android:drawable/ic_media_play" />
<SeekBar android:id="@+id/SeekBar"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:layout_below="@id/ButtonPlayStop" />
</LinearLayout>
Here is the screenshot:
Any one could help me to solve this problem?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您不需要将
WebView
放置在ScrollView
内部,因为它已经知道当其内容大于其视图边界时如何滚动。将滚动视图放置在其他滚动视图内往往会导致不良行为。暂且不管这一点,如果 ScrollView 的内容太小,它就不会自行拉伸以占用额外的空白空间。也就是说,除非特殊标志 (android:fillViewport )被使用。
作为解决方案,我会删除外部的 ScrollView 并尝试让 WebView 自行占据空间。如果您使用
RelativeLayout
作为容器,您可以获得仅具有一层深度的布局:fill_parent
You don't need to place a
WebView
inside of aScrollView
since it already knows how to scroll when its contents are larger than its view boundaries. Placing scrolling views inside other scrolling views tends to lead to undesirable behaviour.Disregarding that for a second, a
ScrollView
will not stretch itself to take up extra empty space if its contents are too small. That is, unless the special flag (android:fillViewport) is used.As a solution, I would remove the outer
ScrollView
and try to make theWebView
take up the space by its own right. If you use aRelativeLayout
as the container you can obtain this layout with only one level of depth:WebView is above them with
fill_parent
in width and height