约束布局响应式设计问题-android(已关闭)

时间:2018-09-09 23:57:04

标签: android constraints android-constraintlayout

我正在使用约束布局来设计UI。问题在于该设计对于5英寸至5.5英寸的屏幕看起来不错(这意味着1080x1920 dpi至1440x2560 dpi),但是当我将3.7英寸的屏幕切换至4.7英寸的屏幕(480x800 dpi至768x1280 dpi)时出现了问题。用户界面的缺点不见了。

那么,如何使用约束布局对所有设备屏幕的UI设计进行响应?

5英寸至5.5英寸的屏幕(即1080x1920 dpi至1440x2560 dpi)

3.7英寸至4.7英寸屏幕(480x800 dpi至768x1280 dpi)

 <?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 :(得分:1)

首先,如果确实不需要,请不要使用固定宽度的高度。您可以使用准则,而不是为所有视图提供开始和结束页边距。检查下面的布局

<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>

答案 1 :(得分:-1)

在不对高度和宽度进行硬编码的情况下,我建议您使用水平线性布局来托管所有类似内容:

<?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>