سؤال

I can't figure out where this left padding is coming from.

My grid is not just uncentered, it appears to be aligned to the right. What's up with this?

Layout:

<RelativeLayout 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:paddingLeft="8dp"
    android:paddingRight="8dp"
    android:paddingTop="8dp"
    android:paddingBottom="8dp">

    <GridView xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/gridView"
        android:numColumns="auto_fit"
        android:columnWidth="240dp"
        android:stretchMode="spacingWidthUniform"
        android:verticalSpacing="22dp"
        android:horizontalSpacing="16dp"
        android:gravity="center_horizontal"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_centerInParent = "true">
    </GridView>

</RelativeLayout>
هل كانت مفيدة؟

المحلول

I believe it is coming from the line

android:stretchMode="spacingWidthUniform"

Remove that and test it out.

نصائح أخرى

Try this tag

 android:gravity="center" in GridView

As

<RelativeLayout 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">

    <GridView xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/gridView"
        android:numColumns="3"// Changed to 3
        android:columnWidth="60dp"
        android:stretchMode="columnWidth"// Added
        android:verticalSpacing="22dp"
        android:horizontalSpacing="16dp"
        android:gravity="center_horizontal"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:gravity="center"
        >
    </GridView>

</RelativeLayout>
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top