Horizo​​ntalScrollView不完全向右滚动

Stu*_*ing 5 android android-layout android-scrollview

我目前的实施

我有一个HorizontalScrollView用XML创建的包含几个LinearLayout孩子的东西.我在下面添加了此代码.

有两个LinearLayout容器用的ID group_one,并group_two和它们在运行时编程方式填充.

我还HorizontalScrollView根据View要插入的对象数量来修复运行时的宽度.

这个解决方案非常适合孩子们在HorizontalScrollView不需要滚动的情况下适应的情况.

问题

一旦我需要滚动(有更多的孩子可以在固定宽度内显示HorizontalScrollView),滚动条将不会一直向右,即使我可以看到子布局的宽度正确,并且我可以看到滚动条不会再进一步​​了.

我的问题

为什么滚动条会进一步向右移动?

我的守则

Horizo​​ntalScrollView 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>
Run Code Online (Sandbox Code Playgroud)

怎么了

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

正确向左滚动

这是右侧滚动错误的图像.比较滚动视图边缘与滚动条停止位置之间的距离.

向右滚动不正确

当我在两端滚动时,这就是我想要的样子.

正确的布局

Stu*_*ing 7

我已经玩了一段时间了,终于找到了解决方案.

我试图将左边距和右边距添加到LinearLayoutID中group_container.但由于某种原因,HorizontalScrollView不尊重这一点,这就是我看到这个问题的原因.

相反,我将左右边距添加到group_onegroup_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>
Run Code Online (Sandbox Code Playgroud)