在scrollview内部具有静态大小的Linearlayout

时间:2015-03-08 08:43:26

标签: android android-layout android-linearlayout android-scrollview

我正在开发和安装android项目,我需要一个静态大小的linearlayout。但是我的linearlayout的高度超过了屏幕尺寸(2000dp),所以我把它放在ScrollView中。

问题是它的高度不是静态的,它的屏幕高度相等。

如何解决这个问题?

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<ScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fillViewport="true" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="2000dp"
        android:orientation="vertical"
        android:weightSum="8" >

        <View
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="4"
            android:background="#880000" />

        <View
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="3"
            android:background="#008800" />

        <View
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:background="#000088" />
    </LinearLayout>
</ScrollView>

输出:

enter image description here

提前致谢

2 个答案:

答案 0 :(得分:0)

您可以尝试更改ScrollView的高度以包装内容

 android:layout_height="wrap_content"

答案 1 :(得分:0)

无论孩子ScrollView如何,

layout_height都会计算孩子的身高。您可以看到measureChildmeasureChildWithMargins的{​​{3}}。他们负责计算子维度。在ScrollView内,他们使用MeasureSpec.UNSPECIFIED为子高度执行计算。

因此,执行您要求的唯一方法就是将LinearLayout包裹在FrameLayout中:

<ScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="2000dp"
            android:orientation="vertical">

            <!-- items here -->

        </LinearLayout>
    </FrameLayout>
</ScrollView>