如何使WebView高度尺寸与屏幕高度相符?

Mil*_*avi 1 android webview android-layout android-gui

如果我的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>
Run Code Online (Sandbox Code Playgroud)

这是截图:

在此输入图像描述

任何人都可以帮我解决这个问题?

ant*_*nyt 8

您不需要放置一个WebView内部,ScrollView因为它已经知道如何在其内容大于其视图边界时滚动.将滚动视图放置在其他滚动视图内往往会导致不良行为.

ScrollView如果它的内容太小,如果不理会这一秒,它将不会伸展自己以占用额外的空白空间.也就是说,除非使用特殊标志(android:fillViewport).

作为一种解决方案,我会移除外部ScrollView并尝试通过自己的方式WebView占据空间.如果使用a RelativeLayout作为容器,则只能使用一个深度级别获得此布局:

  • 播放按钮位于父母的左下角
  • 搜索栏位于播放按钮的底部和右侧
  • WebView的fill_parent宽度和高度都在它们之上

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    
    <Button android:textStyle="bold"
    android:id="@+id/ButtonPlayStop"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:background="@android:drawable/ic_media_play" />
    
    <SeekBar android:id="@+id/SeekBar"
    android:layout_toRightOf="@+id/ButtonPlayStop"
    android:layout_alignParentBottom="true"
    android:layout_height="wrap_content"
    android:layout_width="fill_parent"
    android:layout_below="@id/ButtonPlayStop" />
    
    <WebView android:id="@+id/webview"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_above="@id/ButtonPlayStop" />
    
    </RelativeLayout>
    
    Run Code Online (Sandbox Code Playgroud)