android - Horizo​​ntalScrollView 没有完全向右滚动

标签 android android-layout android-scrollview

我目前的实现

我有一个 Horizo​​ntalScrollView,它是我用 XML 创建的,它包含一些 LinearLayout 子项。我在下面添加了这段代码。

有两个 LinearLayout 容器,id 分别为 group_onegroup_two,它们在运行时以编程方式填充。

我还在运行时根据要插入的 View 对象的数量固定 Horizo​​ntalScrollView 的宽度。

此解决方案非常适用于当 child 无需滚动即可适应 Horizo​​ntalScrollView 时。

问题

只要我需要滚动(在固定宽度 Horizo​​ntalScrollView 内可以显示更多的子项),滚动条就不会一直向右移动,即使我可以看到子布局的宽度正确,我可以看到滚动条不会再移动了。

我的问题

为什么滚动条向右移动会有限制?

我的代码

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>

发生了什么

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

Correct Scroll Left

这是一张错误的向右滚动的图片。比较 ScrollView 边缘与滚动条停止位置之间的距离。

Incorrect Scroll Right

这就是我希望它在两端滚动时的样子。

Correct Layout

最佳答案

我已经研究了一段时间,终于找到了解决方案。

我试图将左右边距添加到 ID 为 group_containerLinearLayout。但是由于某种原因,Horizo​​ntalScrollView 没有遵守这一点,这就是我看到这个问题的原因。

相反,我将左右边距添加到 group_onegroup_two LinearLayouts。现在 Horizo​​ntalScrollView 遵守了这些并且它的功能符合我的预期。这是我修改后的代码。

<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>

关于android - Horizo​​ntalScrollView 没有完全向右滚动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25059518/

相关文章:

android - 用于构建 Android 应用程序的 Gwt

android - Android Chrome 中的 Jquery 错误

Android 显示/隐藏小部件调整大小填充父级

android - 带有动画的Android应用程序的自定义标签栏

android - ScrollView 底部的空白区域

android - 带有 ListView 的 ScrollView 不滚动 - android

android - ScrollView 中的 TabHost 强制它滚动到底部

java - 我们如何更改小部件按钮的图像?

Android:在动态创建的布局中查找 fragment

java - Android - Activity 构造函数与 onCreate