我遇到了以下问题:我实现了一个HorizontalScrollView
,其中在某些情况下包含一个LinearLayout
和一个ImageView
。在这种情况下,图像大约占屏幕宽度的50%。因此,我想将其居中。不幸的是,我找到的唯一居中的方法是在LinearLayout
上使用layout_gravity="center"
。
这是我的xml:
<HorizontalScrollView
android:layout_gravity="center"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<LinearLayout
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:src="@drawable/myImg"
android:layout_width="wrap_content"
android:adjustViewBounds="true"
android:layout_height="150dp"/>
</LinearLayout>
</HorizontalScrollView>
但我需要通过编程来设置layout_gravity
。有人知道我如何实现这个目标,或者是否还有其他方法吗?我在 Google 上找到的所有东西都对我不起作用,比如这个帖子。
谢谢!