【问题标题】:How to fit WebView height size with screen height?如何使 WebView 高度大小与屏幕高度相匹配?
【发布时间】:2011-10-10 19:37:49
【问题描述】:

如果我的 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>

截图如下:

谁能帮我解决这个问题?

【问题讨论】:

    标签: android android-layout webview android-gui


    【解决方案1】:

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

    暂时不考虑,ScrollView 如果内容太小,将不会自行伸展以占用额外的空白空间。也就是说,除非使用特殊标志 (android:fillViewport)。

    作为一种解决方案,我将删除外部ScrollView 并尝试使WebView 自行占用空间。如果你使用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>
      

    【讨论】:

    猜你喜欢
    • 2016-10-03
    • 2017-08-10
    • 2020-03-06
    • 2018-09-03
    • 2011-10-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多