【问题标题】:why isn't the view expand to fill the remain space为什么视图不扩展以填充剩余空间
【发布时间】:2025-12-25 07:50:11
【问题描述】:

我想扩展 webview1 视图以填充剩余空间。使按钮看起来总是在屏幕底部。 我使用 android:layout_weight 属性为单个孩子分配权重。 但它似乎不起作用。我该怎么办,谁能告诉我什么

<?xml version="1.0" encoding="utf-8"?>

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <ScrollView style="@style/ScrollView" >

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:orientation="vertical"
            android:paddingBottom="8dp"
            android:paddingLeft="12dp"
            android:paddingRight="12dp"
            android:paddingTop="8dp" >

            <LinearLayout
                android:layout_width="fill_parent"
                android:layout_height="0dp"
                android:layout_marginTop="12dp"
                android:orientation="vertical"
                android:layout_weight="1" >      <---here assign a weight

                <WebView
                    android:id="@+id/webview1"
                    **style="@style/update_text" />**
            </LinearLayout>

            <!-- h-line -->

            <RelativeLayout
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="12dp" >

                <ImageView
                    android:id="@+id/line_end"
                    style="@style/form_h_line" />
            </RelativeLayout>

            <!-- buttons -->

            <LinearLayout
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="12dp"
                android:orientation="horizontal" >

                <Space
                    android:layout_width="12dp"
                    android:layout_height="wrap_content"/>

                <Button
                    android:id="@+id/button_ok"
                    style="@style/base_button"
                    android:layout_width="12dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="2"
                    android:text="OK"/>

                <Space
                    android:layout_width="12dp"
                    android:layout_height="wrap_content"/>
            </LinearLayout>
        </LinearLayout>
    </ScrollView>

</FrameLayout>

原因是,因为它在Framelayout中工作,所以webview1不能自动展开。

【问题讨论】:

    标签: android attributes autofill


    【解决方案1】:

    fill_parent/match_parentScrollView 中不起作用,原因很明显 - 如果确实如此,ScrollView 将毫无用处。

    您的ScrollView 应将fillViewPort 属性设置为true。如果需要,这会使ScrollView 的孩子扩展到ScrollView 的高度。如果孩子大于屏幕,则该属性无效。

    【讨论】:

      【解决方案2】:

      添加weightSum="" property to your LinearLayout

      所以使用关注并根据需要管理它with wiegtSum and layout_weight 也使用 LinearLayout 而不是 &lt;Space&gt; 标签

            <LinearLayout
                  android:layout_width="fill_parent"
                  android:layout_height="wrap_content"
                  android:layout_marginTop="12dp"
                  android:weightSum="3"
                  android:orientation="horizontal" >
      
                  <LinearLayout
                      android:layout_width="0dp"
                      android:layout_weight="1" 
                      android:layout_height="wrap_content"/>
      
                  <Button
                      android:id="@+id/button_ok"
                      style="@style/base_button"
                      android:layout_width="12dp"
                      android:layout_height="wrap_content"
                      android:layout_weight="1"
                      android:text="OK"/>
                 <LinearLayout
                      android:layout_width="0dp"
                      android:layout_weight="1" 
                      android:layout_height="wrap_content"/>
      
              </LinearLayout>
      

      【讨论】:

      • 谢谢,我的意思是我想将 WebView id/webview1 扩展到剩余空间。现在在通过 URL 内容加载 web 视图之前,该按钮显示在屏幕顶部。所以即使webview中没有内容我也想扩展webview