как центрировать элементы в GridView?

У меня есть требование: введите здесь описание изображения

У меня есть макет, как:

<?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>

ответ: введите здесь описание изображения

пожалуйста, помогите, я пробовал так много вещей.


person Rajiv yadav    schedule 13.06.2013    source источник
comment
вы можете получить ответы с использованием линейного макета вместо сетки?   -  person deadfish    schedule 13.06.2013
comment
@Rajiv yadav Ты нашел решение?   -  person FiXiT    schedule 14.08.2017
comment
тот же вопрос, вы нашли решение?   -  person Luan Si Ho    schedule 19.08.2017


Ответы (2)


вы можете добиться этого, используя атрибут layout_span TableRow. Просто добавь

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);

Надеюсь это поможет...

person CRUSADER    schedule 13.06.2013

Работа с GridView действительно утомительна, и вам трудно получить макет, если вы не хотите использовать TableLayout и TableRow. Ваш вопрос похож на другой вопрос здесь, в StackOverflow, где я уже ответил.

Моя идея состоит в том, чтобы «заставить» GridView иметь точную ширину, то есть сумму ширины ваших столбцов, например: вы можете взять ширину вашего ImageView, который составляет сетку, и умножить ширину * столбцы. Затем вы можете программно установить общую ширину для вашего GrdiView с помощью LayoutParams.

person JJ86    schedule 13.06.2013