HorizontalScrollView不能完全向右滚动

6

我的当前实现方案

我创建了一个XML中的HorizontalScrollView,其中包含几个LinearLayout子项。下面是我添加的代码。

有两个LinearLayout容器,分别为group_onegroup_two,这些都是在运行时动态填充的。

我还会根据将插入的View对象数量,在运行时固定HorizontalScrollView的宽度。

当子项适合于不需要滚动HorizontalScrollView时,这个解决方案效果很好。

问题

一旦需要进行滚动(子项超出了固定宽度的HorizontalScrollView),则即使我可以看到子项布局的正确宽度,也无法将滚动条向右移动到底部。

我的问题

为什么滚动条会受到向右移动的限制?

我的代码

HorizontalScrollView XML

<!-- THIS IS WHERE THE PLUGIN BUTTONS ARE HOUSED -->
    <HorizontalScrollView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:scrollbars="horizontal"
        android:id="@+id/map_plugin_scroll_view"
        android:background="@color/map_plugin_background">

    <!-- Enclosing box to layout the two groups.-->
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:layout_margin="8dp"
        android:id="@+id/group_container">

        <!-- These layouts contain the map plugins. -->
        <LinearLayout
            android:id="@+id/group_one"
            android:orientation="horizontal"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginBottom="4dp"/>

        <LinearLayout
            android:id="@+id/group_two"
            android:orientation="horizontal"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="4dp"/>

    </LinearLayout>

</HorizontalScrollView>

发生了什么

这是一个向左正确滚动的图像。滚动视图的边缘从红色条的右侧开始。注意两者之间的距离。

向左正确滚动

这是向右不正确滚动的图像。比较滚动视图的边缘和滚动条停止的位置之间的距离。

向右不正确滚动

这是我希望滚动到任一端时看到的布局。

正确的布局

2个回答

10
我已经尝试了一段时间,并最终找到了解决方案。 我试图为ID为“group_container”的LinearLayout添加左和右边距。但是由于某种原因,HorizontalScrollView没有尊重这一点,这就是我看到这个问题的原因。 相反,我将左右边距添加到group_one和group_two的LinearLayouts中。现在,HorizontalScrollView尊重了这些内容,并按照我的预期进行操作。以下是我的修改后的代码。
<HorizontalScrollView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:scrollbars="horizontal"
        android:id="@+id/map_plugin_scroll_view"
        android:background="@color/map_plugin_background">

        <!-- Enclosing box to layout the two groups.-->
        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:layout_marginTop="8dp"
            android:layout_marginBottom="8dp"
            android:id="@+id/group_container">

            <!-- These layouts contain the map plugins. -->
            <LinearLayout
                android:id="@+id/group_one"
                android:orientation="horizontal"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginBottom="4dp"
                android:layout_marginLeft="8dp"
                android:layout_marginRight="8dp"/>

            <LinearLayout
                android:id="@+id/group_two"
                android:orientation="horizontal"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="4dp"
                android:layout_marginLeft="8dp"
                android:layout_marginRight="8dp"/>

        </LinearLayout>

    </HorizontalScrollView>

天啊,我差点被 ScrollView 搞疯了,我从没想到还有 HorizontalScrollView... - IgniteCoders

0

将滚动视图的右侧内边距设置为如下所示:

android:paddingRight="20dp"

1
这只是在子元素和滚动视图之间添加了一个间隙。仍然看到相同的问题。 - StuStirling

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接