【问题标题】:Linearlayout with static size inside scrollview滚动视图内具有静态大小的线性布局
【发布时间】:2015-03-08 08:43:26
【问题描述】:

我正在开发一个需要具有静态尺寸的线性布局的 android 项目。但是我的线性布局的高度超过了屏幕尺寸(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>

输出:

提前致谢

【问题讨论】:

  • 如果你是父视图的高度,那你为什么不给分高度像 android:layout_height="665dp"
  • 嗯!因为我将来会根据屏幕尺寸(手机/平板电脑)更改这个 2000

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


【解决方案1】:

你能不能试着改变 ScrollView 的高度来包裹内容

 android:layout_height="wrap_content"

【讨论】:

  • 你可以尝试将 android:layout_height="2000dp" 给滚动视图
【解决方案2】:

ScrollView 计算其孩子的高度,而不管孩子的layout_height。你可以看到measureChildmeasureChildWithMarginssource。他们负责计算子维度。在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>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-04-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多