【发布时间】:2015-02-16 07:39:04
【问题描述】:
我有一个 Android 应用程序,我试图在其中将网格视图以线性布局居中。我不必使用线性布局,但如果可能的话,我更喜欢。
我想在网格视图中设置水平间距(列之间的空间),以及在父级中设置网格视图的中心。
当我在 XML 中使用 android:stretchMode="none" 时,水平间距有效。但是,当我这样做时,网格视图是左对齐的。使用android:stretchMode="columnWidth" 网格视图居中,但水平间距不起作用。
我怎样才能两者兼得?
下面是我的 XML 示例(水平间距有效,居中无效):
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center_horizontal">
<GridView
android:id="@+id/myList"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:numColumns="auto_fit"
android:horizontalSpacing="1dp"
android:columnWidth="400dp"
android:stretchMode="none"
android:layout_gravity="center_horizontal">
</GridView>
</LinearLayout>
【问题讨论】:
标签: android android-layout gridview