53

我想将布局权重分配LinearLayoutScrollView. 但是,ScrollView忽略LinearLayout weightSum.

我的目标是将布局划分为 2、1、1 的权重(总和为 4),但这在ScrollView.

我该如何解决这个布局问题?

主要的.xml

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/scrollView1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <LinearLayout 
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical" 
        android:weightSum="4">

        <LinearLayout android:id="@+id/logo"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical" 
            android:layout_weight="2"
            android:background="#FFFFFF" />

        <LinearLayout android:id="@+id/logo1"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical" 
            android:layout_weight="1"
            android:background="#000000" />


        <LinearLayout android:id="@+id/logobutton"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal" 
            android:layout_weight="1" 
            android:background="#4B4B4B" />

    </LinearLayout>
</ScrollView>
4

5 回答 5

167

我以前遇到过这个问题。只需android:fillViewport="true"在您的 ScrollView 中使用,它就会填满屏幕。

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/scrollView1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:fillViewport="true" >
于 2012-04-25T09:12:49.873 回答
3

这不会像你所做的那样工作。ScrollView的子视图应设置为wrap_content. 如果将其设置为fill_parent,它将填充ScrollView的区域并且从不滚动,因为它不会大于ScrollView

的想法layout_weight是按比例填充特定区域。

您应该将所有子LinearLayouts layout_height设置为任wrap_content一或特定大小(以 dp 为单位),并将父LinearLayout layout_height设置为wrap_content

如前所述,您需要删除额外的

xmlns:android="http://schemas.android.com/apk/res/android"

如果它不是布局的根(第一个)元素。

于 2012-04-25T09:08:17.853 回答
0

以我的经验,fillviewport=true工作有点奇怪。

我通过LinearLayout使用另一个布局(如FrameLayout.

我希望这有帮助。

于 2019-03-10T08:50:14.020 回答
0

在您的 ScrollView 中添加以下行,它将正常工作。

ScrollView 必须只有一个孩子

android:fillViewport="true"
于 2020-01-07T09:37:55.617 回答
0

只需将其放在您的滚动视图中:

android:fillViewport="true"

于 2020-04-12T09:15:44.623 回答