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

问题描述 投票:0回答:2

我正在使用约束布局来设计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>
android constraints android-constraintlayout
2个回答
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
投票

除了没有硬编码高度和宽度,我建议您使用水平线性布局来托管所有内容:

<?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>
© www.soinside.com 2019 - 2024. All rights reserved.