2012-05-07 2 views
4

В принципе, у меня есть представление, которое нужно разбить на сетку 2x4, причем каждая секция равна по размеру (т.е. каждая строка будет 25% от высоты экрана, и каждый столбец будет 50% ширины экрана).Создание сетки, такой как макет в Android

Мой подход состоял в использовании горизонтального LinearLayout с двумя внутренними LinearLayouts с весом 0,5, а затем в каждом из этих LinearLayouts был установлен вес детей до 0,25, так что каждый из них занимает 25% экрана.

Хотя это, кажется, работает, это, видимо, очень плохо сказывается на производительности (см эту тему по причине, почему Why are nested weights bad for performance? Alternatives?)

Существуют ли какие-либо альтернативы для достижения этой цели? Я посмотрел вокруг, но я не могу найти для этого чистое XML-решение.

См ниже пример кода, как у меня есть LinearLayouts и их установки детского

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="horizontal" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:baselineAligned="false" 
    android:weightSum="1.0" 
    > 
    <LinearLayout 
     android:orientation="vertical" 
     android:layout_width="0dip" 
     android:layout_height="fill_parent" 
     android:layout_weight="0.5" 
     android:weightSum="1.0">   
      <ImageView 
       android:layout_width="wrap_content" 
       android:layout_height="0dip" 
       android:src="@drawable/example" 
       android:layout_gravity="center_horizontal" 
       android:layout_weight="0.25" 
      /> 

      <ImageView 
       android:layout_width="wrap_content" 
       android:layout_height="0dip" 
       android:src="@drawable/example" 
       android:layout_gravity="center_horizontal" 
       android:layout_weight="0.25" 
      /> 

      <ImageView 
       android:layout_width="wrap_content" 
       android:layout_height="0dip" 
       android:src="@drawable/example" 
       android:layout_gravity="center_horizontal" 
       android:layout_weight="0.25" 
      /> 

      <ImageView 
       android:layout_width="wrap_content" 
       android:layout_height="0dip" 
       android:src="@drawable/example" 
       android:layout_gravity="center_horizontal" 
       android:layout_weight="0.25" 
      /> 
    </LinearLayout> 

    <LinearLayout 
     android:orientation="vertical" 
     android:layout_width="0dip" 
     android:layout_height="fill_parent" 
     android:layout_weight="0.5" 
     android:weightSum="1.0">   
      <ImageView 
       android:layout_width="wrap_content" 
       android:layout_height="0dip" 
       android:src="@drawable/example" 
       android:layout_gravity="center_horizontal" 
       android:layout_weight="0.25" 
      /> 

      <ImageView 
       android:layout_width="wrap_content" 
       android:layout_height="0dip" 
       android:src="@drawable/example" 
       android:layout_gravity="center_horizontal" 
       android:layout_weight="0.25" 
      /> 

      <ImageView 
       android:layout_width="wrap_content" 
       android:layout_height="0dip" 
       android:src="@drawable/example" 
       android:layout_gravity="center_horizontal" 
       android:layout_weight="0.25" 
      /> 

      <ImageView 
       android:layout_width="wrap_content" 
       android:layout_height="0dip" 
       android:src="@drawable/example" 
       android:layout_gravity="center_horizontal" 
       android:layout_weight="0.25" 
      /> 
    </LinearLayout> 
</LinearLayout> 

ответ

4

Вы можете дать GridLayout спина.

Существует также library, что делает его доступным для устройств с 1.6+.

+0

Я попробую сейчас, спасибо :) – rastating

1

Использование GridView внутри LinearLayout. Check http://developer.android.com/resources/tutorials/views/hello-gridview.html

+0

К сожалению, мне нужно уметь устанавливать количество строк так, чтобы высота автоматически настраивалась, поэтому я не могу использовать GridView в этом конкретном сценарии :( – rastating

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