Wyśrodkuj elementy w widoku GridView


Mam GridView z 6 obrazami w 2 kolumnach. Chcę wyświetlić te 6 obrazów na środku ekranu. Ustawiłem właściwości grawitacji na środek, ale ten środek elementów jest tylko poziomy. GridView zajmuje cały ekran.
<GridView
android:id="@+id/gridview"
android:layout_above="@id/ad" android:layout_alignParentTop="true"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:numColumns="2"
android:verticalSpacing="10dp"
android:horizontalSpacing="10dp"
android:stretchMode="columnWidth"
android:background="#ffffff"
android:gravity="center"
/>

Zaproszony:
Anonimowy użytkownik

Anonimowy użytkownik

Potwierdzenie od:

Oto, co zrobiłem, aby ustawić elementy w widoku siatki na środku (zwróć uwagę na tryb rozciągania, szerokość kolumny, grawitację oraz odstępy w poziomie i pionie):
<LinearLayout xmlns:android="[url=http://schemas.android.com/apk/res/android"]http://schemas.android.com/apk/res/android"[/url]
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:padding="5dp" >
<GridView
android:id="@+id/gridview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:columnWidth="100dp"
android:gravity="center"
android:horizontalSpacing="10dp"
android:numColumns="3"
android:stretchMode="spacingWidthUniform"
android:verticalSpacing="10dp"/>
</LinearLayout>

Zajęło mi trochę czasu, zanim to rozgryzłem, ale majstrując przy tych różnych wartościach, byłem w stanie je skupić.
Anonimowy użytkownik

Anonimowy użytkownik

Potwierdzenie od:

Spróbuj opakować widok GridView w LinearLayout, który ma wyśrodkowane elementy podrzędne i zmień layout_height widoku siatki na „wrap_content”. dawny:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="[url=http://schemas.android.com/apk/res/android"]http://schemas.android.com/apk/res/android"[/url]
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:gravity="center"
android:layout_gravity="center">
<GridView
android:id="@+id/homeGridView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:numColumns="3"
android:verticalSpacing="10dp"
android:horizontalSpacing="10dp"
android:stretchMode="columnWidth"
android:gravity="center"/>
</LinearLayout>
Anonimowy użytkownik

Anonimowy użytkownik

Potwierdzenie od:

Należy ustawić następujące atrybuty:
android:stretchMode="columnWidth"
...i...
android:gravity="center"
Anonimowy użytkownik

Anonimowy użytkownik

Potwierdzenie od:

cóż, jeśli używasz niestandardowego adaptera do powiększania widoku pojedynczego wiersza w widoku siatki, spróbuj dodać

android:gravity="center"

do układu single_row .. mam nadzieję, że to załatwi sprawę :)
to znaczy,
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="[url=http://schemas.android.com/apk/res/android"]http://schemas.android.com/apk/res/android"[/url]
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical" > <ImageView
android:id="@+id/ivClrBlock"
android:layout_width="88dp"
android:layout_height="88dp"
android:layout_marginBottom="5dp"
android:layout_marginTop="5dp"
/> <TextView
android:id="@+id/tvClrName"
android:layout_width="88dp"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:layout_marginTop="5dp"
android:gravity="center"
android:text="@string/color_name"
android:textSize="15sp"/></LinearLayout>
Anonimowy użytkownik

Anonimowy użytkownik

Potwierdzenie od:

Problem pojawił się, gdy zaktualizowałem adapter Gridview o:
gridView = inflater.inflate(R.layout.mobile, null);

do
gridView = inflater.inflate(R.layout.mobile, parent, false);

więc po prostu wróciłem, używając try catch, jeśli stara wersja (null) zawiodła.
Anonimowy użytkownik

Anonimowy użytkownik

Potwierdzenie od:

Spróbuj to zmienić:
android:layout_height="fill_parent"

w
android:layout_height="wrap_content"
Teraz mówisz widokowi siatki, aby całkowicie wypełnił wysokość i szerokość nadrzędną.
Anonimowy użytkownik

Anonimowy użytkownik

Potwierdzenie od:

Mam nadzieję, że to rozwiąże Twój problem:
<LinearLayout
android:layout_gravity="center_horizontal"
android:gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<GridLayout
android:id="@+id/gridlayou4x3"
android:rowCount="4"
android:columnCount="3"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="80"
android:minWidth="25px"
android:minHeight="25px"/>
</LinearLayout>
Anonimowy użytkownik

Anonimowy użytkownik

Potwierdzenie od:

Zauważyłem, że masz tylko zawartość
gridlayout
zawiniętego według
height
i
width
.
android:layout_centerHorizontal="true"

Skoncentruje się na potrzebnym rozmiarze, ale jeśli masz pasującego rodzica. Z technicznego punktu widzenia koncentrujesz się na dużej ilości rodzicielskich białych znaków, które kompensują twoje dzieci.
Anonimowy użytkownik

Anonimowy użytkownik

Potwierdzenie od:

Oto moja odpowiedź:
<LinearLayout xmlns:android="[url=http://schemas.android.com/apk/res/android"]http://schemas.android.com/apk/res/android"[/url]
android:id="@id/linearLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<GridView
android:id="@+id/gridView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:columnWidth="100dip"
android:gravity="center"
android:numColumns="3"
android:stretchMode="spacingWidthUniform"
android:verticalSpacing="10dip"/>
</LinearLayout>

Nie używaj
padding
ani
horizontalSpacing
, chyba że musisz, ponieważ może to powodować problemy z innymi widokami.
Zwróć również uwagę, że
fill_parent
jest teraz przestarzały.

Aby odpowiedzieć na pytania, Zaloguj się lub Zarejestruj się