2015-10-24 2 views
0

Я пытаюсь использовать GridView, чтобы показать 4 части контента. В приведенном ниже коде это содержимое является только ImageView, но позже будет добавлен текстовый файл. Я создал 2x2 GridView, и я ожидал, что пространство экрана будет разделено поровну на все 4 ячейки сетки, но этого не происходит. Если установлено значение fill_parent, ячейка принимает весь экран (например, изображение ниже), если установлено значение wrap_content, ячейки заполняются рядом с одним из углов. Как я могу получить ячейку для равномерного распределения только с файлом макета XML?GridView равное распределение пространства

enter image description here

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <GridLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:rowCount="2" 
     android:orientation="horizontal" 
     android:columnCount="2"> 

     <LinearLayout 
      android:orientation="horizontal" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:layout_rowSpan="1" 
      android:layout_columnSpan="1" 
      android:layout_column="0" 
      android:layout_row="0"> 

      <ImageView 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:id="@+id/imageView2" 
       android:src="@drawable/placeholder" /> 
     </LinearLayout> 

     <LinearLayout 
      android:orientation="horizontal" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:layout_columnSpan="1" 
      android:layout_rowSpan="1" 
      android:layout_column="1" 
      android:layout_row="0"> 

      <ImageView 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:id="@+id/imageView" 
       android:src="@drawable/placeholder" /> 
     </LinearLayout> 

     <LinearLayout 
      android:orientation="horizontal" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_rowSpan="1" 
      android:layout_columnSpan="1" 
      android:layout_column="1" 
      android:layout_row="1"> 

      <ImageView 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:id="@+id/imageView3" 
       android:src="@drawable/placeholder" /> 
     </LinearLayout> 

     <LinearLayout 
      android:orientation="horizontal" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:layout_rowSpan="1" 
      android:layout_columnSpan="1" 
      android:layout_column="0" 
      android:layout_row="1"> 

      <ImageView 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:id="@+id/imageView4" 
       android:src="@drawable/placeholder" /> 
     </LinearLayout> 

    </GridLayout> 
</LinearLayout> 
+0

Любая причина, по которой вы не просто используете 'LinearLayout' с весами? – tachyonflux

+0

Если у меня тоже, то я это сделаю, но я считаю, что это решение совсем не изящно, он чувствует себя так же, как грубый, заставляющий вас выйти из проблемы. – AmiguelS

ответ

1

Это должно работать:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <GridLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:rowCount="2" 
     android:orientation="horizontal" 
     android:columnCount="2"> 

     <LinearLayout 
      android:orientation="horizontal" 
      android:layout_columnWeight="1" 
      android:layout_rowWeight="1" 
      android:layout_gravity="fill"> 

      <ImageView 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:id="@+id/imageView2" 
       android:src="@drawable/placeholder" /> 
     </LinearLayout> 

     <LinearLayout 
      android:orientation="horizontal" 
      android:layout_rowWeight="1" 
      android:layout_columnWeight="1" 
      android:layout_gravity="fill"> 

      <ImageView 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:id="@+id/imageView" 
       android:src="@drawable/placeholder" /> 
     </LinearLayout> 

     <LinearLayout 
      android:orientation="horizontal" 
      android:layout_columnWeight="1" 
      android:layout_rowWeight="1" 
      android:layout_gravity="fill"> 

      <ImageView 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:id="@+id/imageView3" 
       android:src="@drawable/placeholder" /> 
     </LinearLayout> 

     <LinearLayout 
      android:orientation="horizontal" 
      android:layout_rowWeight="1" 
      android:layout_columnWeight="1" 
      android:layout_gravity="fill"> 

      <ImageView 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:id="@+id/imageView4" 
       android:src="@drawable/placeholder" /> 
     </LinearLayout> 

    </GridLayout> 
</LinearLayout> 

Я удалил layout_height, layout_width, layout_rowSpan и layout_columnSpan и использовали вес с layout_gravity="fill" вместо этого.

+1

Только то, что мне нужно! Благодаря! – AmiguelS

Смежные вопросы