2013-09-05 3 views
0

Есть ли способ получить изображения, затрагивающие, как на этом изображении, не догадываясь о высотах каждого горизонтального LinearLayout?Android: Как получить этот макет

Can i do this without guessing the layout heights?

Как вы можете видеть из моего XML я должен был угадать высоту LinearLayout на 144dp, чтобы получить этот эффект, есть лучший способ, который будет работать на различных размерах экрана?

<LinearLayout 
     android:id="@+id/LinearLayout" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:orientation="vertical" > 

     <LinearLayout 
      android:id="@+id/LinearLayout01" 
      android:layout_width="match_parent" 
      android:layout_height="144dp" > 

      <ImageView 
       android:id="@+id/ImageView01" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:layout_weight="1" 
       android:src="@drawable/ic_sport" /> 

      <ImageView 
       android:id="@+id/ImageView02" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:layout_weight="1" 
       android:src="@drawable/ic_science" /> 
     </LinearLayout> 

     <LinearLayout 
      android:id="@+id/LinearLayout02" 
      android:layout_width="match_parent" 
      android:layout_height="144dp" 
      android:layout_gravity="fill_horizontal" > 

      <ImageView 
       android:id="@+id/imageView03" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:layout_weight="1" 
       android:src="@drawable/ic_science" /> 

      <ImageView 
       android:id="@+id/imageView04" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:layout_weight="1" 
       android:src="@drawable/ic_sport" /> 
     </LinearLayout> 

     <LinearLayout 
      android:id="@+id/LinearLayout03" 
      android:layout_width="match_parent" 
      android:layout_height="144dp" 
      android:layout_gravity="fill_horizontal" > 

      <ImageView 
       android:id="@+id/ImageView05" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:layout_weight="1" 
       android:src="@drawable/ic_sport" /> 

      <ImageView 
       android:id="@+id/ImageView06" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:layout_weight="1" 
       android:src="@drawable/ic_science" /> 
     </LinearLayout> 
    </LinearLayout> 

Если возможно, я предпочел бы иметь возможность сделать это в формате XML. Изображения квадратные, поэтому они должны заполнять ширину и все еще быть пропорционально. Я могу почти сделать это с fitXY, но это просто растягивает их ширину, а не их высоту.

+1

Как вы угадываете высоту линейного макета? Вы имеете в виду, что вы угадываете высоту того, что линейный макет будет зависеть от высоты изображений в них? Можете ли вы поместить каждый горизонтальный линейный макет в вертикальную линейную компоновку и добиться того, чего хотите? –

+0

Я увеличил высоту LinearLayout01 до тех пор, пока изображения не заполнили LinearLayout (линейная компоновка стала квадратной) – Edd

+0

@cskoala Использование вертикальных линий LinearLayouts - хороший вариант. Он заполняет экран вертикально небольшими промежутками между двумя вертикальными LinearLayouts. Я могу, вероятно, компенсировать это, используя scaleXY – Edd

ответ

-1

Хотя образы являются не квадратное (хотя его совсем незаметным) мне удалось решить эту проблему с помощью 2 Вертикальный LinearLayout с вместо 3-х горизонтальных и с помощью ScaleType=fitXY, чтобы заполнить оставшиеся пробелы, оставив меня с этим:

Final Layout

из кода:

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

    <LinearLayout 
     android:id="@+id/LinearLayout01" 
     android:layout_width="fill_parent" 
     android:layout_height="match_parent" 
     android:layout_weight="1" 
     android:orientation="vertical" 
     android:weightSum="3" > 

     <ImageView 
      android:id="@+id/ImageView03" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:layout_weight="1" 
      android:scaleType="fitXY" 
      android:src="@drawable/ic_science" /> 

     <ImageView 
      android:id="@+id/ImageView01" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:layout_weight="1" 
      android:scaleType="fitXY" 
      android:src="@drawable/ic_sport" /> 

     <ImageView 
      android:id="@+id/ImageView02" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:layout_weight="1" 
      android:scaleType="fitXY" 
      android:src="@drawable/ic_science" /> 

    </LinearLayout> 

    <LinearLayout 
     android:id="@+id/LinearLayout02" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_weight="1" 
     android:orientation="vertical" 
     android:weightSum="3" > 

     <ImageView 
      android:id="@+id/ImageView04" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:layout_weight="1" 
      android:scaleType="fitXY" 
      android:src="@drawable/ic_sport" /> 

     <ImageView 
      android:id="@+id/imageView03" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:layout_weight="1" 
      android:scaleType="fitXY" 
      android:src="@drawable/ic_science" /> 

     <ImageView 
      android:id="@+id/imageView04" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:layout_weight="1" 
      android:scaleType="fitXY" 
      android:src="@drawable/ic_sport" /> 

    </LinearLayout> 

</LinearLayout> 
2

Вы должны использовать GridView вместо многих LinearLayouts.

Если вы хотите сохранить ваш код, обновить это:

Ваш главный макет должен иметь это свойство:

android:layout_height="match_parent" 

Другие "Макет Упаковщики" должен иметь:

android:layout_height="wrap_content" 
android:orientation="horizontal" 

Ваше изображение должно иметь:

android:layout_height="wrap_content" 

Вы работаете только с квадратными изображениями?

Я обновил свой код и не пробовал, но он должен работать:

<LinearLayout 
     android:id="@+id/LinearLayout" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:orientation="vertical" > 

     <LinearLayout 
      android:id="@+id/LinearLayout01" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:orientation="horizontal"> 

      <ImageView 
       android:id="@+id/ImageView01" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:layout_weight="1" 
       android:src="@drawable/ic_sport" /> 

      <ImageView 
       android:id="@+id/ImageView02" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:layout_weight="1" 
       android:src="@drawable/ic_science" /> 
     </LinearLayout> 

     <LinearLayout 
      android:id="@+id/LinearLayout02" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:orientation="horizontal" > 

      <ImageView 
       android:id="@+id/imageView03" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:layout_weight="1" 
       android:src="@drawable/ic_science" /> 

      <ImageView 
       android:id="@+id/imageView04" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:layout_weight="1" 
       android:src="@drawable/ic_sport" /> 
     </LinearLayout> 

     <LinearLayout 
      android:id="@+id/LinearLayout03" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_gravity="fill_horizontal" 
      android:orientation="horizontal" > 

      <ImageView 
       android:id="@+id/ImageView05" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:layout_weight="1" 
       android:src="@drawable/ic_sport" /> 

      <ImageView 
       android:id="@+id/ImageView06" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:layout_weight="1" 
       android:src="@drawable/ic_science" /> 
     </LinearLayout> 
    </LinearLayout> 
+0

, если вы используете 'layout_weight', вы должны положить' 0dp' для 'layout_height' или' layout_width', в зависимости от того, что вы хотите, чтобы он срабатывал. – Eluvatar

+0

Все мои изображения квадратные да, мне нужно только работать с квадратными изображениями – Edd

+0

Ваш код не работает :(они возвращаются к касанию по вертикали, но с большими зазорами горизонтально – Edd

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