GridView 的行为不符合预期。 这screenshot显示 GridView(在横向模式下)向左冲洗。 我希望它居中。
这是 GridView 的 XML 布局。
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/templatelandscape"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<GridView
android:id="@+id/commandsbarlandscape"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1.0"
android:layout_alignParentTop="true"
android:layout_centerInParent="true"
android:padding="0dp"
android:verticalSpacing="2dp"
android:horizontalSpacing="2dp"
android:numColumns="auto_fit"
android:columnWidth="52dp"
android:stretchMode="spacingWidth"
android:gravity="fill_horizontal" />
</LinearLayout>
我做错了什么?
最佳答案
我不知道你是否解决了你的问题(我希望是的,我也很晚才回答这篇文章!),但这里有一个线索:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/mainLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center" >
<GridView
android:id="@+id/gridview2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:columnWidth="90dp"
android:numColumns="auto_fit"
android:stretchMode="spacingWidth" >
</GridView>
</LinearLayout>
不幸的是,只有当网格上有 1 个内容时,这项工作才有效。 也许有人可以改进它!
关于android - 如何让 GridView 在其 LinearLayout 父级中居中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1544429/