Android GridView Форматирование - PullRequest
       0

Android GridView Форматирование

0 голосов
/ 09 февраля 2012

У меня есть GridView, которым я хочу быть 4 TextView одинакового размера в 2 x 2 GridView. Я хочу, чтобы TextViews были в центре их соответствующего квадранта GridView.

Моя первая проблема: ничто не центрируется. Все тяготеет к левому краю. Во-вторых, хотя я могу довести ширину столбца до 50% доступной ширины, я не могу понять, как настроить высоту строки на 50% доступной высоты. Кажется, вес работает только в одном направлении.

<GridView  
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:id="@+id/MyGrid"
   android:layout_width="fill_parent"
   android:layout_height="fill_parent"
   android:padding="5dp"
   android:verticalSpacing="5dp"
   android:horizontalSpacing="10dp"
   android:numColumns="2"
   android:columnWidth="400dp"
   android:stretchMode="columnWidth"
   android:layout_weight=".5"
   android:gravity="center">  
</GridView>

Портрет enter image description here

Пейзажный вид enter image description here

1 Ответ

1 голос
/ 09 февраля 2012

То, что вы, возможно, хотите, это использовать TableLayout вместо. GridView аналогично ListView, в котором есть предметы, но в этом случае вы хотите иметь только 4 предмета, а не неопределенное количество.

GridView

GridView - это группа ViewGroup, которая отображает элементы в двумерной сетке с возможностью прокрутки. Элементы сетки автоматически вставляются в макет с помощью ListAdapter.

TableLayout

TableLayout - это ViewGroup, которая отображает дочерние элементы View в строках и столбцах.

Вот фрагмент кода xml, который будет работать:

<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/MyGrid"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_weight=".5"
    android:gravity="center"
    android:padding="5dp"
    android:stretchColumns="0,1" >

    <TableRow
        android:id="@+id/tableRow1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1" >

        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:text="Large Text"
            android:textAppearance="?android:attr/textAppearanceLarge" />

        <TextView
            android:id="@+id/textView2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:text="Large Text"
            android:textAppearance="?android:attr/textAppearanceLarge" />
    </TableRow>

    <TableRow
        android:id="@+id/tableRow2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1" >

        <TextView
            android:id="@+id/textView3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:text="Large Text"
            android:textAppearance="?android:attr/textAppearanceLarge" />

        <TextView
            android:id="@+id/textView4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:text="Large Text"
            android:textAppearance="?android:attr/textAppearanceLarge" />
    </TableRow>

</TableLayout>
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...