简体   繁体   English

即使在设置android:layout_width =“ match_parent”时,Horizo​​ntalScrollView的子级也不匹配其父级视图的宽度

[英]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. 同样,在将大小更改为固定大小时,我注意到Horizo​​ntalScrollView实际上仅表现为垂直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与Horizo​​tanl 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. 我不认为您需要尝试实现带有嵌套LinearLayoutHorizontalScrollView 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.

相关问题 使视图的 layout_width=“match_parent” 并使高度等于其宽度的简单方法是什么? - What's a simple way to make a view's layout_width=“match_parent” and make the height equal to its width? 生成的约束布局中的Android layout_width=&quot;match_parent&quot;? - Android layout_width="match_parent" in generated constraint layout? TextView字幕无需android:layout_width =“ match_parent” - TextView marquee WITHOUT android:layout_width=“match_parent” Android RelativeLayout和子元素,layout_width =“match_parent” - Android RelativeLayout and childs with layout_width=“match_parent” 如果由android设置,则getWidth()返回0:layout_width =“match_parent” - getWidth() returns 0 if set by android:layout_width=“match_parent” android中“ match_parent”的用途:layout_width / height用于Android顶视图 - Purpose of “match_parent” in android:layout_width/height for top view in Android 如何在Recycler View中将卡片的初始layout_width设置为Match_Parent? - How to set initial layout_width of cards in Recycler View to Match_Parent? 如何从代码中设置android:layout_width =“match_parent”? - How to set android:layout_width=“match_parent” from code? CardView layout_width =“match_parent”与父级RecyclerView宽度不匹配 - CardView layout_width=“match_parent” does not match parent RecyclerView width RecyclerView 项目宽度 layout_width=&quot;match_parent&quot; 与父项不匹配 - RecyclerView item width layout_width=“match_parent” does not match parent
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM