Ограничение макета адаптивной задачи дизайна - Android (Закрыто) - PullRequest
0 голосов
/ 10 сентября 2018

Я использую макет ограничения для разработки пользовательского интерфейса.И проблема в том, что дизайн выглядит хорошо для экранов от 5 дюймов до 5,5 дюймов (это означает 1080x1920 точек на дюйм до 1440x2560 точек на дюйм), но проблема возникает, когда я переключаю 3,7 дюйма на 4,7 дюйма (480x800 точек на дюйм на 768x1280 точек на дюйм), а затемНедостатки пользовательского интерфейса исчезли.

Итак, как я могу адаптировать дизайн моего пользовательского интерфейса для всех экранов устройств, используя макет ограничения?

от 5 до 5,5 дюймов (это означает 1080x1920 точек на дюймдо 1440x2560 точек на дюйм)

от 3,7 до 4,7 дюймов (от 480x800 до 768x1280 точек на дюйм)

 <?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.constraint.ConstraintLayout
        android:id="@+id/constraintLayout2"
        android:layout_width="0dp"
        android:layout_height="209dp"
        app:layout_constraintVertical_weight="1"
        app:layout_constraintBottom_toTopOf="@+id/constraintLayout"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <ImageView
            android:id="@+id/imageView4"
            android:layout_width="208dp"
            android:layout_height="0dp"
            android:layout_marginBottom="16dp"
            android:layout_marginTop="8dp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:srcCompat="@drawable/lion_king" />
    </android.support.constraint.ConstraintLayout>

    <android.support.constraint.ConstraintLayout
        android:id="@+id/constraintLayout"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:background="@color/colorGre"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/constraintLayout2"
        app:layout_constraintVertical_weight="3">

        <EditText
            android:id="@+id/editText5"
            android:layout_width="298dp"
            android:layout_height="52dp"
            android:layout_marginEnd="8dp"
            android:layout_marginStart="8dp"
            android:layout_marginTop="24dp"
            android:background="@drawable/react_edittext"
            android:ems="10"
            android:inputType="textPersonName"
            android:text="Name"
            android:textAlignment="center"
            android:textAppearance="?android:textAppearanceMedium"
            android:textColor="@android:color/white"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.494"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <EditText
            android:id="@+id/editText6"
            android:layout_width="298dp"
            android:layout_height="52dp"
            android:layout_marginEnd="8dp"
            android:layout_marginStart="8dp"
            android:layout_marginTop="12dp"
            android:background="@drawable/react_edittext"
            android:ems="10"
            android:inputType="textPersonName"
            android:text="Name"
            android:textAlignment="center"
            android:textAppearance="?android:textAppearanceMedium"
            android:textColor="@android:color/white"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.494"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/editText5" />

        <TextView
            android:id="@+id/textView"
            android:layout_width="204dp"
            android:layout_height="32dp"
            android:layout_marginEnd="8dp"
            android:layout_marginStart="8dp"
            android:layout_marginTop="20dp"
            android:text="TextView"
            android:textAlignment="center"
            android:textAppearance="?android:textAppearanceMedium"
            android:textColor="@android:color/white"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.497"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/editText6" />

        <Button
            android:id="@+id/button"
            android:layout_width="330dp"
            android:layout_height="wrap_content"
            android:layout_marginEnd="8dp"
            android:layout_marginStart="8dp"
            android:layout_marginTop="20dp"
            android:background="@drawable/react_button"
            android:backgroundTint="@color/colorAccent"
            android:text="Button"
            android:textAppearance="?android:textAppearanceMedium"
            android:textColor="@android:color/white"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.492"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/textView" />

        <Button
            android:id="@+id/button2"
            android:layout_width="330dp"
            android:layout_height="wrap_content"
            android:layout_marginEnd="8dp"
            android:layout_marginStart="8dp"
            android:layout_marginTop="12dp"
            android:background="@drawable/react_button"
            android:backgroundTint="@color/colorPrimary"
            android:text="Button"
            android:textAppearance="?android:textAppearanceMedium"
            android:textColor="@android:color/white"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.492"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/button" />

        <TextView
            android:id="@+id/textView2"
            android:layout_width="189dp"
            android:layout_height="32dp"
            android:layout_marginBottom="8dp"
            android:layout_marginEnd="8dp"
            android:layout_marginStart="8dp"
            android:layout_marginTop="20dp"
            android:text="TextView"
            android:textAlignment="center"
            android:textAppearance="?android:textAppearanceMedium"
            android:textColor="@android:color/white"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/button2"
            app:layout_constraintVertical_bias="0.0" />
    </android.support.constraint.ConstraintLayout>

</android.support.constraint.ConstraintLayout>

Ответы [ 2 ]

0 голосов
/ 25 октября 2018

Во-первых, не используйте фиксированную высоту ширины, если она действительно не нужна. Вы можете использовать указания вместо того, чтобы указывать начальные и конечные поля для всех видов. Проверьте макет ниже

<android.support.constraint.ConstraintLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/c_ffc72c">

<android.support.constraint.ConstraintLayout
    android:id="@+id/constraintLayout2"
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:background="@color/white"
    app:layout_constraintBottom_toTopOf="@+id/constraintLayout"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_weight="1">

    <ImageView
        android:id="@+id/imageView4"
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:layout_marginTop="8dp"
        android:layout_marginBottom="16dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:srcCompat="@drawable/ic_creditcard" />
</android.support.constraint.ConstraintLayout>

<android.support.constraint.ConstraintLayout
    android:id="@+id/constraintLayout"
    android:layout_width="0dp"
    android:layout_height="0dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toStartOf="@+id/guidelineEnd"
    app:layout_constraintStart_toStartOf="@+id/guidelineStart"
    app:layout_constraintTop_toBottomOf="@+id/constraintLayout2"
    app:layout_constraintVertical_weight="3">


    <EditText
        android:id="@+id/editText5"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginTop="24dp"
        android:ems="10"
        android:inputType="textPersonName"
        android:text="Name"
        android:textAlignment="center"
        android:textAppearance="?android:textAppearanceMedium"
        android:textColor="@android:color/white"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <EditText
        android:id="@+id/editText6"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginTop="12dp"
        android:ems="10"
        android:inputType="textPersonName"
        android:text="Name"
        android:textAlignment="center"
        android:textAppearance="?android:textAppearanceMedium"
        android:textColor="@android:color/white"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/editText5" />

    <TextView
        android:id="@+id/textView"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:text="TextView"
        android:textAlignment="center"
        android:textAppearance="?android:textAppearanceMedium"
        android:textColor="@android:color/white"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/editText6" />

    <Button
        android:id="@+id/button"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:backgroundTint="@color/colorAccent"
        android:text="Button"
        android:textAppearance="?android:textAppearanceMedium"
        android:textColor="@android:color/white"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/textView" />

    <Button
        android:id="@+id/button2"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginTop="12dp"
        android:backgroundTint="@color/colorPrimary"
        android:text="Button"
        android:textAppearance="?android:textAppearanceMedium"
        android:textColor="@android:color/white"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/button" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:layout_marginEnd="8dp"
        android:text="TextView"
        android:textAlignment="center"
        android:textAppearance="?android:textAppearanceMedium"
        android:textColor="@android:color/white"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/button2"
        app:layout_constraintVertical_bias="0.0" />
</android.support.constraint.ConstraintLayout>

<android.support.constraint.Guideline
    android:id="@+id/guidelineStart"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    app:layout_constraintGuide_begin="20dp" />

<android.support.constraint.Guideline
    android:id="@+id/guidelineEnd"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    app:layout_constraintGuide_end="20dp" />

</android.support.constraint.ConstraintLayout>
0 голосов
/ 10 сентября 2018

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

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 
   xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:app="http://schemas.android.com/apk/res-auto"
   xmlns:tools="http://schemas.android.com/tools"
   android:layout_width="match_parent"
   android:layout_height="match_parent">

<LinearLayout
    android:layout_width="0dp"
    android:layout_height="0dp">

    <ImageView
        android:id="@+id/imageView4"
        android:layout_width="208dp"
        android:layout_height="0dp"
        android:layout_marginBottom="16dp"
        android:layout_marginTop="8dp"
        app:srcCompat="@drawable/lion_king" />

    <EditText
        android:id="@+id/editText5"
        android:layout_width="298dp"
        android:layout_height="52dp"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="24dp"
        android:background="@drawable/react_edittext"
        android:ems="10"
        android:inputType="textPersonName"
        android:text="Name"
        android:textAlignment="center"
        android:textAppearance="?android:textAppearanceMedium"
        android:textColor="@android:color/white"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.494"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <EditText
        android:id="@+id/editText6"
        android:layout_width="298dp"
        android:layout_height="52dp"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="12dp"
        android:background="@drawable/react_edittext"
        android:ems="10"
        android:inputType="textPersonName"
        android:text="Name"
        android:textAlignment="center"
        android:textAppearance="?android:textAppearanceMedium"
        android:textColor="@android:color/white"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.494"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/editText5" />

    <TextView
        android:id="@+id/textView"
        android:layout_width="204dp"
        android:layout_height="32dp"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="20dp"
        android:text="TextView"
        android:textAlignment="center"
        android:textAppearance="?android:textAppearanceMedium"
        android:textColor="@android:color/white"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.497"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/editText6" />

    <Button
        android:id="@+id/button"
        android:layout_width="330dp"
        android:layout_height="wrap_content"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="20dp"
        android:background="@drawable/react_button"
        android:backgroundTint="@color/colorAccent"
        android:text="Button"
        android:textAppearance="?android:textAppearanceMedium"
        android:textColor="@android:color/white"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.492"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/textView" />

    <Button
        android:id="@+id/button2"
        android:layout_width="330dp"
        android:layout_height="wrap_content"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="12dp"
        android:background="@drawable/react_button"
        android:backgroundTint="@color/colorPrimary"
        android:text="Button"
        android:textAppearance="?android:textAppearanceMedium"
        android:textColor="@android:color/white"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.492"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/button" />
    <TextView
        android:id="@+id/textView2"
        android:layout_width="189dp"
        android:layout_height="32dp"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="20dp"
        android:text="TextView"
        android:textAlignment="center"
        android:textAppearance="?android:textAppearanceMedium"
        android:textColor="@android:color/white"
        app:layout_constraintVertical_bias="0.0" />
   </LinearLayout>
</android.support.constraint.ConstraintLayout>
...