我有简单的布局:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<GridView
android:id="@+id/photo_browser_grid_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:gravity="center"
android:horizontalSpacing="5dp"
android:verticalSpacing="5dp"
android:stretchMode="none"
android:numColumns="auto_fit"
android:columnWidth="100dp">
</GridView>
我想将我的 GridView 置于相对布局的中心。我的间距总是固定的。结果我让 GridView 左对齐。我怎样才能将它放在父级中?
最佳答案
对于布局中的GridView,android:numColumns="auto_fit"
与android:layout_width="wrap_content"
冲突。这意味着:要计算形成多少列,需要宽度,但要找到宽度,我们需要环绕多少列,这是一种循环依赖。
您可以使用 android:stretchMode
让列填满空白空间。
关于java - 在相对布局中居中 GridView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13660329/