2016-06-22 4 views
1

У меня есть простая компоновка с 4 различными уровнями/режимами, которые вы можете играть. Проблема заключается в том, когда я просмотреть расположение на различных размеров экрана не появится то же самое: ImageКак сделать мой макет шкалы на разных размерах экрана?

Вот код макета:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" android:layout_height="match_parent" 
    android:background="#000000"> 

    <TextView 
     android:layout_width="match_parent" 
     android:layout_height="120dp" 
     android:text="play" 
     android:gravity="center" 
     android:textSize="50sp" 
     android:id="@+id/one" 
     android:layout_alignParentTop="true" 
     android:background="@drawable/mode1background" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentRight="true" 
     android:textColor="#000000" /> 

    <TextView 
     android:layout_width="match_parent" 
     android:layout_height="120dp" 
     android:text="play" 
     android:gravity="center" 
     android:textSize="50sp" 
     android:id="@+id/two" 
     android:background="@drawable/mode2background" 
     android:layout_below="@+id/one" 
     android:layout_alignParentRight="true" 
     android:layout_alignParentLeft="true" 
     android:textColor="#000000" /> 

    <TextView 
     android:layout_width="match_parent" 
     android:layout_height="120dp" 
     android:text="play" 
     android:gravity="center" 
     android:textSize="50sp" 
     android:id="@+id/three" 
     android:background="@drawable/mode3background" 
     android:layout_below="@+id/two" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentRight="true" 
     android:textColor="#000000" /> 

    <TextView 
     android:layout_width="match_parent" 
     android:layout_height="120dp" 
     android:text="play" 
     android:gravity="center" 
     android:textSize="50sp" 
     android:id="@+id/four" 
     android:background="@drawable/mode4background" 
     android:layout_below="@+id/three" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentRight="true" 
     android:textColor="#000000" /> 

</RelativeLayout> 

Как я могу сделать каждый TextView быть 1/4 от размера экрана.

+0

попробовать это http://stackoverflow.com/questions/29025843/android-devices-with-different-height-takes-same-layout-folder/29026085#29026085 –

+0

Вы пробовали с 'LinearLayout', потому что у него есть свойство' android: weight = {value} ' –

ответ

3

Вы можете использовать вертикальную LinearLayout с android:layout_weight атрибутов ,

Что-то вроде этого:

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

    <TextView 
     android:id="@+id/one" 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:layout_weight="0.25" 
     android:background="@drawable/mode1background" 
     android:gravity="center" 
     android:textColor="#000000" 
     android:textSize="50sp"/> 

    <TextView 
     android:id="@+id/two" 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:layout_weight="0.25" 
     android:background="@drawable/mode2background" 
     android:gravity="center" 
     android:textColor="#000000" 
     android:textSize="50sp"/> 

    <TextView 
     android:id="@+id/three" 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:layout_weight="0.25" 
     android:background="@drawable/mode3background" 
     android:gravity="center" 
     android:textColor="#000000" 
     android:textSize="50sp"/> 

    <TextView 
     android:id="@+id/four" 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:layout_weight="0.25" 
     android:background="@drawable/mode4background" 
     android:gravity="center" 
     android:textColor="#000000" 
     android:textSize="50sp"/> 

</LinearLayout> 

Проверьте в руководстве разработчика по LinearLayout и Layout Вес: https://developer.android.com/guide/topics/ui/layout/linear.html

+0

Должно ли my layout_weight быть 0.25 или 1, как предлагают другие ответы? В чем разница, потому что он работает с 0,25 и 1 как свет. –

+0

Это не имеет значения, так как они одинаковы. Я просто предпочитаю, когда сумма моих весов равна 1 :) – earthw0rmjim

+0

Разве это не так, когда сумма равна 4 (с весами 1) и 1 (с 0,25) –

0

создать следующие директории в resourcefolder и поместить различные изображения разрешения фона в соответствующих каталогах

вытяжка-ldpi

вытяжки-MDPI

вытяжка-ИПЧР

вытяжки-xhdpi

drawable-xxhdpi

вытяжка-xxxhdpi

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

2

Изменение родительского макета для LinearLayout с вертикальной ориентацией. Затем установите высоту каждого ребенка 0dp и веса 1

Пример:

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

    <TextView 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:layout_weight="1" 
     android:gravity="center" 
     android:text="1"/> 

    <TextView 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:layout_weight="1" 
     android:gravity="center" 
     android:text="2"/> 

    <TextView 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:layout_weight="1" 
     android:gravity="center" 
     android:text="3"/> 

    <TextView 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:layout_weight="1" 
     android:gravity="center" 
     android:text="4"/> 

</LinearLayout> 
+0

Должно ли мое layout_weight быть 1 или 0.25, как предлагают другие ответы? В чем разница, потому что он работает с 0,25 и 1 как свет. –

+0

Нет никакой разницы, если вы не определяете weightSum для родителя. – Niko

0

Как вы хотите, чтобы занимать всю высоту устройства вам нужно будет использовать линейную компоновку с вертикальной oreintation и давая атрибут «layout_weight '= 1 для всех TextView. Пример кода

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="#000000" 
    android:orientation="vertical"> 

    <TextView 
     android:id="@+id/one" 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentRight="true" 
     android:layout_alignParentTop="true" 
     android:layout_weight="1" 
     android:background="@drawable/mode1background" 
     android:gravity="center" 
     android:text="play" 
     android:textColor="#000000" 
     android:textSize="50sp" /> 

    <TextView 
     android:id="@+id/two" 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentRight="true" 
     android:layout_below="@+id/one" 
     android:layout_weight="1" 
     android:background="@drawable/mode2background" 
     android:gravity="center" 
     android:text="play" 
     android:textColor="#000000" 
     android:textSize="50sp" /> 

    <TextView 
     android:id="@+id/three" 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentRight="true" 
     android:layout_below="@+id/two" 
     android:layout_weight="1" 
     android:background="@drawable/mode3background" 
     android:gravity="center" 
     android:text="play" 
     android:textColor="#000000" 
     android:textSize="50sp" /> 

    <TextView 
     android:id="@+id/four" 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentRight="true" 
     android:layout_below="@+id/three" 
     android:layout_weight="1" 
     android:background="@drawable/mode4background" 
     android:gravity="center" 
     android:text="play" 
     android:textColor="#000000" 
     android:textSize="50sp" /> 

</LinearLayout>