2014-02-02 2 views
0

Я пытаюсь создать макет, который всегда будет отображать 2 изображения, разделяя длину экрана одинаково на половину, не оставляя пробелов на экране (даже если изображения обрезаны в центре).Расположение Android с двумя изображениями, оставляющими пробелы внизу

До сих пор у меня есть следующий код, но это оставляет много пустого пробела в нижней части экрана. Причина, по которой я использую «RelativeLayout» в «LinearLayout», заключается в том, что я хочу, чтобы мое представление text1 попало в нижнюю часть моего изображения 1 (перекрывая изображение1).

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:orientation="vertical" > 

<RelativeLayout 
    android:layout_width="match_parent" 
    android:layout_height="230dp" 
    android:layout_marginBottom="3dp" > 

    <ImageView 
     android:id="@+id/img1" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:clickable="true" 
     android:contentDescription="@string/picture" 
     android:scaleType="centerCrop" /> 

    <TextView 
     android:id="@+id/text1" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_alignParentBottom="true" 
     android:gravity="center" 
     android:padding="5dp" 
     android:textColor="#fff" /> 

</RelativeLayout> 

<RelativeLayout 
    android:layout_width="match_parent" 
    android:layout_height="230dp" 
    android:layout_marginBottom="3dp" > 

    <ImageView 
     android:id="@+id/img2" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:clickable="true" 
     android:contentDescription="@string/picture" 
     android:scaleType="centerCrop" /> 

</RelativeLayout> 

ответ

2

Вы жестко прописывать высоту до 230dp для каждого макета, которые могут быть причиной проблемы. В линейном макете вы должны использовать весы для равномерного распределения площади экрана.

Использования Android: layout_weight = "1" в обеих относительных расположениях

Посмотрите здесь для получения дополнительной информации

Android: 2 relative layout divided in half screen

+0

Спасибо! Это работает. – KT100

1

макета, описывает простой, взвешенный LinearLayout. Просто добавьте следующий атрибут к вашему LinearLayout:

android:weightSum="2" 

Тогда для каждого RelativeLayout, изменить атрибут высоты к:

android:layout_height="0px" 

И добавьте следующий атрибут к тем же RelativeLayout с:

android:layout_weight="1" 
+0

Спасибо! Это работает, но из моего понимания weightSum нам не нужно указывать его, пока это сумма весов детей. – KT100

0

попробуйте использовать компоновку фреймов, если вы хотите наложить текстовое изображение на изображение и использовать макет макета на макете фрейма

Пример кода для разметки XML

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:orientation="vertical" > 

<FrameLayout 
    android:layout_width="match_parent" 
    android:layout_height="0dp" 
    android:layout_marginBottom="3dp" 
    android:layout_weight="0.5" > 

    <ImageView 
     android:id="@+id/img1" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:clickable="true" 
     android:scaleType="centerCrop" /> 

    <TextView 
     android:id="@+id/text1" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:text="Test" 
     android:gravity="center" 
     android:layout_gravity="center_vertical|bottom" 
     android:padding="5dp" 
     android:textColor="@android:color/black" /> 
</FrameLayout> 

<FrameLayout 
    android:layout_width="match_parent" 
    android:layout_height="0dp" 
    android:layout_marginBottom="3dp" 
    android:layout_weight="0.5" > 

    <ImageView 
     android:id="@+id/img2" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:clickable="true" 
     android:scaleType="centerCrop" /> 
</FrameLayout> 

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