[英]HorizontalScrollView's child not matching it's parent view's width even on setting android:layout_width=“match_parent”
I want to display a horizontal list of scrollable buttons at top of the phone screen containing 30 items, for this purpose I am using a HorizontalScrollView with a LinearLayout with "horizontal" orientation as it's child but the linear layout is not taking up the entire phone width even on setting its width as "match-parent". 我想在电话屏幕顶部显示包含30个项目的可滚动按钮的水平列表,为此,我使用的是LinearScrollView,它是“水平”方向的LinearLayout,因为它是孩子,但是线性布局不会占用整个电话宽度,即使将其宽度设置为“匹配父级”也是如此。 Here's the code :
这是代码:
<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>
Here is shown in the image that linear layout is not taking up the entire space and LinearLayout is only covering some of the space. 如图所示,线性布局不占据整个空间,而LinearLayout仅覆盖某些空间。 Also on changing the size to a fixed size, I noticed that the HorizontalScrollView was actually behaving like a vertical Scroll View only.
同样,在将大小更改为固定大小时,我注意到HorizontalScrollView实际上仅表现为垂直Scroll View。
NOTE: Also if there is an alternative way to display a horizontal list of buttons with 30 items with numbers from 1 to 30 on it, please suggest it. 注意:另外,如果还有其他方法可以显示水平按钮列表,其中包含30个项目,其上的数字从1到30,请提出建议。
为了轻松实现这一点,您可以将Recyclerview与Horizotanl Layout Manager结合使用。
recycler_view.setLayoutManager(new LinearLayoutManager(MainActivity.this, LinearLayoutManager.HORIZONTAL, false));
I don't think you need a HorizontalScrollView
with a nested LinearLayout
for what you are trying to achieve. 我不认为您需要尝试实现带有嵌套
LinearLayout
的HorizontalScrollView
。 It would be a better idea to simply use a horizontal RecyclerView
if your button layouts are similar and the functionality of the buttons is similar too. 如果您的按钮布局相似并且按钮的功能也相似,那么最好只使用水平
RecyclerView
。 Refer to this answer for help https://stackoverflow.com/a/40584425/9119277 请参阅此答案以获取帮助https://stackoverflow.com/a/40584425/9119277
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.