中心GridView水平

时间:2013-12-19 11:20:00

标签: android gridview alignment

我有一个GridView,其中包含4个动态添加的项目。我可以垂直居中但不能水平居中。试过一切......

main.xml中

...
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:layout_gravity="center" >

        <GridView
        android:id="@+id/grid"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:columnWidth="90dp"
        android:numColumns="auto_fit"
        android:padding="10dp"
        android:verticalSpacing="10dp"
        android:horizontalSpacing="10dp"
        android:stretchMode="spacingWidthUniform"
        android:gravity="center"
        android:listSelector="@drawable/card_button" >

        </GridView>

    </LinearLayout>
... 

Items.xml

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/btn"
    android:layout_width="match_parent"
    android:layout_height="90dp"
    android:orientation="vertical"
    android:gravity="center" >

            <ImageButton
                android:id="@+id/menuItem_img"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/light"
                android:contentDescription="@string/item"
                android:background="#00000000"
                android:tint="#222222"
                android:paddingTop="8dp"
                android:paddingBottom="4dp" />

            <TextView
                android:id="@+id/menuItem_txt"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textColor="#222222"
                android:paddingBottom="8dp"
                android:text="@string/item"
                android:textSize="18sp"
                android:singleLine="true" />

</LinearLayout>

我不希望有固定数量的列,因此android:numColumns =“auto_fit”必须保留。

1 个答案:

答案 0 :(得分:2)

我发现无法将GridView与.xml水平居中 所以我必须以编程方式进行...这个答案对我有很大帮助:https://stackoverflow.com/questions/6141910/android-center-gridview-horizontally

我必须做一些改动才能让它发挥作用:

GridView Adapter.java:

  public Adapter(Context context, int width, int height, float density, GridView grid) {
        this.context = context;
        width_ = width; //get metrics.widthPixels from MainActivtiy.java
        height_ = height; //get metrics.heightPixels from MainActivtiy.java
        density_ = density; //get metrics.scaledDensity from MainActivtiy.java
        grid_ = grid; //get GridView

        Rect p = new Rect();
        grid_.getSelector().getPadding(p);
        int selectorPadding = p.left + p.right;
        int mSizePx = (int) Math.floor(90 * density_); //90 is my column width
        int mSpacingPx = (int) Math.floor(10 * density_); //10 is space between columns
        int numColumns = (int) Math.floor(1f * (width_ - selectorPadding + mSpacingPx) / (mSizePx + mSpacingPx));

        // CHANGES
        int numbOfCol = menuValues.length; //menuValues (Array[]) are items you are adding to GridView
        int contentWidth = 0;
        //the most important part
        if (numColumns > numbOfCol) {
            contentWidth = numbOfCol * mSizePx;
            contentWidth += (numbOfCol - 1) * mSpacingPx;
            contentWidth += selectorPadding;
        } else {
            contentWidth = numColumns * mSizePx;
            contentWidth += (numColumns - 1) * mSpacingPx;
            contentWidth += selectorPadding;
        }           
        int slack = width_ - contentWidth;
        //
        grid_.setNumColumns(numColumns); //set calculated number of collumns
        grid_.setColumnWidth(mSizePx); //90
        grid_.setVerticalSpacing(mSpacingPx); //10
        grid_.setHorizontalSpacing(mSpacingPx); //10

        //changed
        grid_.setPadding(slack / 2, mSpacingPx, slack / 2, mSpacingPx); //I set top and bottom padding to 10dp (mSpacingPx)

    }

Main.xml(已更改):

...
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:layout_gravity="center" >

        <GridView
        android:id="@+id/grid"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:stretchMode="none"
        android:gravity="center"
        android:listSelector="@drawable/card_button"
        android:drawSelectorOnTop="true" >

        </GridView>

    </LinearLayout>
...