【发布时间】:2017-12-28 15:29:05
【问题描述】:
我想在手机屏幕顶部显示一个可滚动按钮的水平列表,其中包含 30 个项目,为此,我使用带有“水平”方向的 LinearLayout 的 HorizontalScrollView,因为它是子级,但线性布局没有占用即使将其宽度设置为“匹配父级”,整个手机的宽度也是如此。这是代码:
<HorizontalScrollView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:layout_gravity="center">
<GridView
android:id="@+id/gridView_horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:stretchMode="columnWidth" />
</LinearLayout>
</HorizontalScrollView>
从图中可以看出,线性布局并没有占据整个空间,而 LinearLayout 只是覆盖了一部分空间。同样在将大小更改为固定大小时,我注意到 HorizontalScrollView 实际上表现得像一个垂直滚动视图。
注意:另外,如果有另一种方法来显示按钮的水平列表,其中包含 30 个项目,其上的数字从 1 到 30,请提出建议。
【问题讨论】:
-
您不能将 GridView 放在水平滚动视图中。您可以创建具有水平滚动方向的回收站视图。 mRecyclerView.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, true));
标签: android android-layout horizontalscrollview