2010-08-30 4 views
2

Застрял на этом в течение нескольких дней, все просто не работает с тем, как я настраиваю это. У меня есть большая сетка ImageViews, которые имеют одинаковый размер. Он состоит из горизонтального LinearLayout и внутри него, 5 вертикальных LinearLayouts (первое изображение).Android Linear/RelativeLayout размер с ImageViews

Что я хочу (и мне все равно, как это использовать RelativeLayout, Linear или Tables), если бы я должен был сказать, что изображение # 2 на большее изображение (в частности, 3x3 из меньших) эффективно «перезаписать» эти изображения (как показано на рисунке 2).

Я попытался сделать это, установив «перезаписанные» изображения (3,4,7,8,9,12,13,14) на setVisibility (GONE), который отлично работает, но затем второй вертикальный LinearLayout расширился чтобы соответствовать размеру нового изображения, которое я не хочу. Если я попытаюсь установить его на fill_parent, хотя он сквозит размер изображения. В результате я получаю третью картину.

Все связанный XML-код (коды идентификаторов совпадают на изображении):

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:orientation="horizontal"> 
<LinearLayout 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical"> 
    <ImageView android:id="@+id/gs01" 
     android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:layout_gravity="top" 
     /> 
    <ImageView android:id="@+id/gs06" 
     android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:layout_gravity="top" 
     /> 
    <ImageView android:id="@+id/gs11" 
     android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:layout_gravity="top" 
     /> 
    <ImageView android:id="@+id/gs16" 
     android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:layout_gravity="top" 
     /> 
</LinearLayout> 


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical"> 
    <ImageView android:id="@+id/gs02" 
     android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:layout_gravity="top" 
     /> 
    <ImageView android:id="@+id/gs07" 
     android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:layout_gravity="top" 
     /> 
    <ImageView android:id="@+id/gs12" 
     android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:layout_gravity="top" 
     /> 
    <ImageView android:id="@+id/gs17" 
     android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:layout_gravity="top" 
     /> 
</LinearLayout> 


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical"> 
    <ImageView android:id="@+id/gs03" 
     android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:layout_gravity="top" 
     /> 
    <ImageView android:id="@+id/gs08" 
     android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:layout_gravity="top" 
     /> 
    <ImageView android:id="@+id/gs13" 
     android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:layout_gravity="top" 
     /> 
    <ImageView android:id="@+id/gs18" 
     android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:layout_gravity="top" 
     /> 
</LinearLayout> 

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical"> 
    <ImageView android:id="@+id/gs04" 
     android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:layout_gravity="top" 
     /> 
    <ImageView android:id="@+id/gs09" 
     android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:layout_gravity="top" 
     /> 
    <ImageView android:id="@+id/gs14" 
     android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:layout_gravity="top" 
     /> 
    <ImageView android:id="@+id/gs19" 
     android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:layout_gravity="top" 
     /> 
</LinearLayout> 

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical"> 
    <ImageView android:id="@+id/gs05" 
     android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:layout_gravity="top" 
     /> 
    <ImageView android:id="@+id/gs10" 
     android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:layout_gravity="top" 
     /> 
    <ImageView android:id="@+id/gs15" 
     android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:layout_gravity="top" 
     /> 
    <ImageView android:id="@+id/gs20" 
     android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:layout_gravity="top" 
     /> 
</LinearLayout> 

</LinearLayout> 

Если бы я сделать это с помощью RelativeLayouts, я бегу в проблемы, когда я setVisibility (Gone) с позиции может очень хорошо ссылаться на отсутствующий вид. Установка его в INVISIBLE просто оставляет пустое пространство, когда то, что я действительно хочу, должно быть размером 0px размером 0px.

Любая помощь вообще будет оценена по достоинству; Кажется, что бы я ни старался, что-то всегда с ним не так, и это сводит меня с ума.

alt text

ответ

2

Наконец-то он работал с использованием RelativeLayout и якорей. Анкеры устанавливаются быть размер квадрата 1x1 сетки, то у вас есть еще один набор представлений изображений AlignLeft и AlignTop на позиции, которую вы хотите, например:

<?xml version="1.0" encoding="utf-8"?> 

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/relay" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:orientation="horizontal"> 

    <ImageView android:id="@+id/a_t1" 
     android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:layout_gravity="top|left" 
     /> 
    <ImageView android:id="@+id/a_t2" 
     android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:layout_gravity="top|left" 
android:layout_toRightOf="@id/a_t1" 
     /> 
    <ImageView android:id="@+id/a_t3" 
     android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:layout_gravity="top|left" 
android:layout_toRightOf="@id/a_t2" 
     /> 
    <ImageView android:id="@+id/a_t4" 
     android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:layout_gravity="top|left" 
android:layout_toRightOf="@id/a_t3" 
     /> 
    <ImageView android:id="@+id/a_t5" 
     android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:layout_gravity="top|left" 
android:layout_toRightOf="@id/a_t4" 
     /> 


     <ImageView android:id="@+id/a_t6" 
     android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:layout_gravity="top|left" 
android:layout_below="@id/a_t1" 
     /> 
    <ImageView android:id="@+id/a_t7" 
     android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:layout_gravity="top|left" 
android:layout_toRightOf="@id/a_t6" 
android:layout_below="@id/a_t2" 
     /> 
    <ImageView android:id="@+id/a_t8" 
     android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:layout_gravity="top|left" 
android:layout_toRightOf="@id/a_t7" 
android:layout_below="@id/a_t3" 
     /> 
    <ImageView android:id="@+id/a_t9" 
     android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:layout_gravity="top|left" 
android:layout_toRightOf="@id/a_t8" 
android:layout_below="@id/a_t4" 
     /> 
    <ImageView android:id="@+id/a_t10" 
     android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:layout_gravity="top|left" 
android:layout_toRightOf="@id/a_t9" 
android:layout_below="@id/a_t5" 
     /> 



     <ImageView android:id="@+id/a_t11" 
     android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:layout_gravity="top|left" 
android:layout_below="@id/a_t6" 
     /> 
    <ImageView android:id="@+id/a_t12" 
     android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:layout_gravity="top|left" 
android:layout_toRightOf="@id/a_t11" 
android:layout_below="@id/a_t7" 
     /> 
    <ImageView android:id="@+id/a_t13" 
     android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:layout_gravity="top|left" 
android:layout_toRightOf="@id/a_t12" 
android:layout_below="@id/a_t8" 
     /> 
    <ImageView android:id="@+id/a_t14" 
     android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:layout_gravity="top|left" 
android:layout_toRightOf="@id/a_t13" 
android:layout_below="@id/a_t9" 
     /> 
    <ImageView android:id="@+id/a_t15" 
     android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:layout_gravity="top|left" 
android:layout_toRightOf="@id/a_t14" 
android:layout_below="@id/a_t10" 
     /> 



<ImageView android:id="@+id/a_t16" 
     android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:layout_gravity="top|left" 
android:layout_below="@id/a_t11" 
     /> 
    <ImageView android:id="@+id/a_t17" 
     android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:layout_gravity="top|left" 
android:layout_toRightOf="@id/a_t16" 
android:layout_below="@id/a_t12" 
     /> 
    <ImageView android:id="@+id/a_t18" 
     android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:layout_gravity="top|left" 
android:layout_toRightOf="@id/a_t17" 
android:layout_below="@id/a_t13" 
     /> 
    <ImageView android:id="@+id/a_t19" 
     android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:layout_gravity="top|left" 
android:layout_toRightOf="@id/a_t18" 
android:layout_below="@id/a_t14" 
     /> 
    <ImageView android:id="@+id/a_t20" 
     android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:layout_gravity="top|left" 
android:layout_toRightOf="@id/a_t19" 
android:layout_below="@id/a_t15" 
     /> 

    <ImageView android:id="@+id/gs01" 
     android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:layout_gravity="top|left" 
android:layout_alignLeft="@id/a_t1" 
android:layout_alignTop="@id/a_t1" 
     /> 

</RelativeLayout> 

Так что если вы хотите, чтобы положить что-то на # 2, как мой Например, измените layout_alignLeft и layout_alignTop на id gs01 как @ id/a_t2, и он будет работать. Возникает проблема с установкой setVisibility (GONE), которая удаляет точку привязки, используемую для вычисления положения пространств сетки.

Теперь это было сложно. = (

+0

Сложно! О. Но рад, что ты смог заставить его работать! – kcoppock

2

Хм, я вернусь к этому, когда я вернусь домой и попробовать некоторые вещи, но я бы рекомендовал пробовать TableLayout с 5 строк, 5 столбцов. Вы должны иметь возможность задать диапазон строк и столбцов в коде (чтобы представление занимало более одной ячейки). Это то, что я постараюсь первым.

This post выглядит полезным.

+0

Спасибо за помощь. Проблема, с которой я столкнулась с TableLayout, заключается в том, что, хотя вы можете делать COLSPAN, вы не можете делать ROWSPAN, поэтому в примере, который я дал первоначально, изображение # 1 будет растянуто (если fill_parent) или там будет пустое пространство, где # 6 и # 11 (если wrap_content). – Dororo

+0

А, хорошо. Тогда это хороший вопрос. Я еще никогда не использовал GridView, но похоже, что это может быть возможным решением. У меня был Eclipse здесь ...) – kcoppock

+0

Моя единственная проблема с GridView заключается в том, что я не думаю, что это разрешит макет, который я хочу, так как он обрезает/изменяет размеры изображений, чтобы убедиться, что он создает сетку, t думаю, что это позволит изображению охватить несколько «квадратов сетки». Я изучаю использование базы TableLayout, которая хранит изображения-заполнители, а затем использует RelativeLayout для привязки больших изображений к w hichever я хочу, но у меня возникли проблемы с указанием «android: toLeftOf = '@ ID/some_id» в коде, а не в XML. Любая помощь? – Dororo

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