HorizontalScrollView 只能拥有一个子View,所以,使用HorizontalScrollView 会配合布局使用,通常使用LinearLayout,然后LinearLayout内置多个子View。
我在使用过程中,如果使用默认情况,会出现添加的布局从左到右挨个排列。
如果设置LinearLayout的
android:layout_gravity=”center_horizontal”,
当子View不满一屏时是居中分布,当超过一屏时,会发现左侧的第一个View显示不全,所以这个属性设置存在问题。
我使用下列方法解决这个问题,HorizontalScrollView宽为wrap_content,不为match_parent。并将HorizontalScrollView设置为布局水平居中,添加android:gravity=”center_horizontal”属性,让子布局在HorizontalScrollView中也居中。
<HorizontalScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
android:gravity="center_horizontal">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<View
.../>
<View
.../>
</LinearLayout>
</HorizontalScrollView>
这样就可以把View居中显示了,也不会因为布局超过一屏而出现被遮挡现象。