【问题标题】:scrollview not working if inside layout has margin如果内部布局有边距,滚动视图不起作用
【发布时间】:2014-08-19 08:28:39
【问题描述】:

我在使用 ScrollView 时遇到了奇怪的问题,它包含一个带有 topMargin 的 relativeLayout

<ScrollView
    android:id="@+id/scroll_view"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:descendantFocusability="beforeDescendants"
    android:fillViewport="true"
    android:focusableInTouchMode="true">

    <RelativeLayout
        android:id="@+id/cp_editor_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@android:color/white"
        android:layout_marginTop="270dp"
        >
    ...

此代码示例不起作用。滚动在大约 20 像素后停止。如果我要删除 margin_top 属性,那么滚动会按预期工作。

感谢您的帮助

【问题讨论】:

    标签: android android-layout scrollview


    【解决方案1】:

    我不明白 topMargin 停止滚动的问题。但是,为了实现所需的边距并保持滚动功能,我可以想到两种解决方案:

    1) 添加一个与您想要的边距具有相同高度的无关视图,并将其放在您的相对布局(cp_editor_layout)上方。它看起来像这样:

    <ScrollView
        android:id="@+id/scroll_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:descendantFocusability="beforeDescendants"
        android:fillViewport="true"
        android:focusableInTouchMode="true">
    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical" >
    
            <View
                android:layout_width="match_parent"
                android:layout_height="270dp" />
    
            <RelativeLayout
                android:id="@+id/cp_editor_layout"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="@android:color/white" >
    
            </RelativeLayout>
    
        </LinearLayout>
    

    2) 为滚动视图提供上边距,因为无论如何滚动都不需要该空间。如果您要进行某种类型的过度滚动,则需要将 ScrollView 子类化。

    希望这对你有所帮助:)

    【讨论】:

    • 嗯,您的第一个解决方案(添加视图)实际上就是我所做的。由于过度滚动,您的第二个解决方案不起作用。有办法解决这个问题。我正在寻找的是为什么它甚至是一个问题。但是感谢您的帮助
    • 顺便说一句,您的解决方案不起作用。您必须将 View 和 Relative 包装在 LinerLayout 或其他布局中,因为 SCrollView 只能有一个孩子。另外我想补充一点,这个解决方案增加了大量不必要的复杂性。现在我有 4 个布局的深度
    • 对,你需要另一个包装器。我同意这不是最优雅的解决方案,但至少它确实有效。抱歉,我无法提供更多帮助。
    猜你喜欢
    • 2021-06-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-30
    • 1970-01-01
    相关资源
    最近更新 更多