[英]What's a simple way to make a view's layout_width=“match_parent” and make the height equal to its width?
[英]HorizontalScrollView's child not matching it's parent view's width even on setting android:layout_width=“match_parent”
我想在電話屏幕頂部顯示包含30個項目的可滾動按鈕的水平列表,為此,我使用的是LinearScrollView,它是“水平”方向的LinearLayout,因為它是孩子,但是線性布局不會占用整個電話寬度,即使將其寬度設置為“匹配父級”也是如此。 這是代碼:
<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實際上僅表現為垂直Scroll View。
注意:另外,如果還有其他方法可以顯示水平按鈕列表,其中包含30個項目,其上的數字從1到30,請提出建議。
為了輕松實現這一點,您可以將Recyclerview與Horizotanl Layout Manager結合使用。
recycler_view.setLayoutManager(new LinearLayoutManager(MainActivity.this, LinearLayoutManager.HORIZONTAL, false));
我不認為您需要嘗試實現帶有嵌套LinearLayout
的HorizontalScrollView
。 如果您的按鈕布局相似並且按鈕的功能也相似,那么最好只使用水平RecyclerView
。 請參閱此答案以獲取幫助https://stackoverflow.com/a/40584425/9119277
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.