我目前的实现
我有一个 HorizontalScrollView
,它是我用 XML 创建的,它包含一些 LinearLayout
子项。我在下面添加了这段代码。
有两个 LinearLayout
容器,id 分别为 group_one
和 group_two
,它们在运行时以编程方式填充。
我还在运行时根据要插入的 View
对象的数量固定 HorizontalScrollView
的宽度。
此解决方案非常适用于当 child 无需滚动即可适应 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>
发生了什么
这是向左滚动的正确图像。 ScrollView 的边缘从红色条的右侧开始。注意两者之间的距离。
这是一张错误的向右滚动的图片。比较 ScrollView 边缘与滚动条停止位置之间的距离。
这就是我希望它在两端滚动时的样子。
最佳答案
我已经研究了一段时间,终于找到了解决方案。
我试图将左右边距添加到 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>
关于android - HorizontalScrollView 没有完全向右滚动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25059518/