XML-макет Android Studio не выравнивается - PullRequest
0 голосов
/ 17 марта 2019

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

Вот код для макета

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/colorWhite"
    tools:context="com.example.ansel.companionapp.Main_menu">

    <ImageView
        android:id="@+id/logo"
        android:layout_width="150dp"
        android:layout_height="150dp"
        android:layout_alignParentEnd="true"
        android:layout_marginTop="40dp"
        android:layout_marginEnd="113dp"
        android:src="@drawable/companionapp" />


    <TextView
        android:id="@+id/infoText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/logo"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="24dp"
        android:paddingTop="20dp"
        android:text="Large Text"
        android:textColor="@color/colorPrimary"
        android:textSize="18dp" />

    <Button
        android:id="@+id/TPS_button"
        android:layout_width="146dp"
        android:layout_height="wrap_content"
        android:layout_below="@+id/infoText"
        android:layout_alignStart="@+id/infoText"
        android:layout_marginStart="-25dp"
        android:layout_marginTop="53dp"
        android:background="@drawable/button_shape"
        android:onClick="TPS"
        android:text="@string/ThirdPersonShooter"
        android:textColor="@color/colorWhite" />

    <Button
        android:id="@+id/TDS_Button"
        android:layout_width="146dp"
        android:layout_height="wrap_content"
        android:layout_below="@+id/TPS_button"
        android:layout_alignStart="@+id/infoText"
        android:layout_marginStart="-23dp"
        android:layout_marginTop="38dp"
        android:background="@drawable/button_shape"
        android:onClick="TwoDS"
        android:text="@string/TwoDimensionalShooter"
        android:textColor="@color/colorWhite" />


</RelativeLayout>

Вот мой желаемый и фактический макет.

Desired Layout

Actual layout

1 Ответ

2 голосов
/ 17 марта 2019

Я удалил все отрицательные поля и выравнивания и добавил для всех представлений:

android:layout_centerHorizontal="true"

Так проверьте это:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/colorWhite"
    tools:context="com.example.ansel.companionapp.Main_menu">

    <ImageView
        android:id="@+id/logo"
        android:layout_width="150dp"
        android:layout_height="150dp"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="40dp"
        android:src="@drawable/companionapp" />

    <TextView
        android:id="@+id/infoText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/logo"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="24dp"
        android:paddingTop="20dp"
        android:text="Large Text"
        android:textColor="@color/colorPrimary"
        android:textSize="18dp" />

    <Button
        android:id="@+id/TPS_button"
        android:layout_width="146dp"
        android:layout_height="wrap_content"
        android:layout_below="@+id/infoText"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="53dp"
        android:background="@drawable/button_shape"
        android:onClick="TPS"
        android:text="@string/ThirdPersonShooter"
        android:textColor="@color/colorWhite" />

    <Button
        android:id="@+id/TDS_Button"
        android:layout_width="146dp"
        android:layout_height="wrap_content"
        android:layout_below="@+id/TPS_button"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="38dp"
        android:background="@drawable/button_shape"
        android:onClick="TwoDS"
        android:text="@string/TwoDimensionalShooter"
        android:textColor="@color/colorWhite" />
</RelativeLayout>
...