Android 如何在GridView中居中图元?

Android 如何在GridView中居中图元?,android,android-gridview,baseadapter,Android,Android Gridview,Baseadapter,我有一个要求: 我的布局如下: <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/relativelayout" android:layout_width="fill_parent"

我有一个要求:

我的布局如下:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:id="@+id/relativelayout"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:orientation="vertical"
                android:background="@drawable/category_bg">

    <RelativeLayout
            android:id="@+id/headerlinearlayout"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:background="@drawable/category_header"

            >

        <ImageView
                android:id="@+id/logosmall"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/logo_small"
                android:layout_marginLeft="10dp"
                android:layout_centerVertical="true"

                />

        <TextView
                android:id="@+id/textview"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="A Cafe With Arabian Food"
                android:layout_marginLeft="10dp"
                android:layout_gravity="center"
                android:textSize="11pt"
                android:textColor="@color/white"
                android:layout_toRightOf="@+id/logosmall"
                android:layout_centerVertical="true"
                />

        <TextView
                android:id="@+id/choosetxtview"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Choose a Category"
                android:textColor="@color/white"
                android:textSize="11pt"
                android:layout_marginLeft="10dp"
                android:layout_gravity="center"
                android:layout_alignParentRight="true"
                android:layout_centerVertical="true"
                android:layout_marginRight="10dp"
                />
    </RelativeLayout>
    <!--  <LinearLayout


        android:id="@+id/textlayout"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/headerlinearlayout"
        android:gravity="center"
        android:layout_marginTop="5dp"
        >


    </LinearLayout> -->

    <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"

            android:layout_above="@+id/footerlinearlayout"
            android:layout_marginTop="5dip"
            android:layout_below="@+id/headerlinearlayout"
            android:layout_marginBottom="5dip">

        <GridView
                android:id="@+id/gridview"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:columnWidth="120dip"

                android:horizontalSpacing="20dp"
                android:numColumns="3"
                android:paddingTop="5dip"
                android:scrollbars="none"
                android:stretchMode="columnWidth"
                android:gravity="center"
                android:listSelector="@android:color/transparent"
                android:cacheColorHint="@android:color/transparent"
                android:verticalSpacing="20dp"/>

    </LinearLayout>


    <RelativeLayout
            android:id="@+id/footerlinearlayout"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:background="@drawable/category_footer"
            android:layout_alignParentBottom="true"

            >

        <ImageButton
                android:id="@+id/backbtn_iv"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/backbtn"
                android:layout_marginLeft="10dp"
                android:layout_gravity="center"
                android:layout_centerVertical="true"
                />

        <ImageButton
                android:id="@+id/homebtn_iv"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerVertical="true"
                android:layout_gravity="center"
                android:layout_marginLeft="30dp"
                android:layout_toRightOf="@+id/backbtn_iv"
                android:background="@drawable/homebtn"/>


        <ImageButton
                android:id="@+id/helpbtn_iv"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerInParent="true"
                android:layout_gravity="center"
                android:layout_centerVertical="true"
                android:background="@drawable/helpbtn"
                android:layout_alignParentRight="true"
                android:layout_marginRight="80dp"

                />


        <ImageView
                android:id="@+id/iv_poweredby"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerHorizontal="true"
                android:layout_centerVertical="true"
                android:src="@drawable/powerd_by"/>


    </RelativeLayout>


</RelativeLayout>

答复是:


请帮助我尝试了这么多东西。

您可以通过使用
TableRow
的layout\u span属性来实现这一点。加上

android:layout_span="2"
TableRow的子元素中

你也可以像这样通过编程来完成

//theChild in this case is the child of TableRow
TableRow.LayoutParams params = (TableRow.LayoutParams) theChild.getLayoutParams();
params.span = 2; //amount of columns you will span
theChild.setLayoutParams(params);

希望这有帮助……

处理GridView确实是一件令人头痛的事情,如果您不想使用TableLayout和TableRow,那么很难获得您的布局。 你的问题和我已经回答过的类似


我的想法是“强制”GridView有一个精确的宽度,即列宽度的总和,例如:您可以获取组成网格的ImageView的宽度,并将宽度*列相乘。然后,您可以通过LayoutParams以编程方式设置GrdiView的总宽度。

欢迎您使用线性布局而不是栅格视图来回答问题?@Rajiv yadav您找到解决方案了吗?这里的问题与此相同,您找到解决方案了吗?