Android studio ConstraintLayout大小调整问题

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Android studio ConstraintLayout大小调整问题相关的知识,希望对你有一定的参考价值。

我在android Studio中还很陌生,我遇到了ConstraintLayout的问题。即,我正在尝试使我的应用程序完全响应,并且在将组件的宽度完美地调整为任何屏幕尺寸的同时,我不知道如何以相同的方式调整高度。相反,我的布局垂直离开了屏幕。

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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"
    tools:context="com.skorupinski.lunchtime.LoginActivity">


    <androidx.constraintlayout.widget.ConstraintLayout
        android:id="@+id/info_box"
        android:layout_width="match_parent"
        android:layout_height="254dp"
        android:background="@drawable/gradient_bg"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintBottom_toTopOf="@+id/login_form"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <ImageButton
            android:id="@+id/back_button"
            android:layout_width="45dp"
            android:layout_height="45dp"
            android:background="@drawable/back_arrow3"
            android:contentDescription="back_arrow"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintHorizontal_bias="0.005"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintVertical_bias="0.013" />

        <ImageView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginLeft="133dp"
            android:layout_marginTop="64dp"
            android:layout_marginRight="133dp"
            android:layout_marginBottom="64dp"
            android:src="@drawable/logo"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.498"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintVertical_bias="0.593" />

    </androidx.constraintlayout.widget.ConstraintLayout>

    <androidx.constraintlayout.widget.ConstraintLayout
        android:id="@+id/login_form"
        android:layout_width="match_parent"
        android:layout_height="478dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintHorizontal_bias="1.0"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/info_box"
        app:layout_constraintVertical_bias="0.0">

        <EditText
            android:id="@+id/login_input"
            android:layout_width="match_parent"
            android:layout_height="45dp"
            android:layout_marginLeft="49dp"
            android:layout_marginRight="49dp"
            android:layout_marginBottom="20dp"
            app:layout_constraintBottom_toTopOf="parent"
            app:layout_constraintHorizontal_bias="0.491"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toBottomOf="parent"
            app:layout_constraintVertical_bias="0.779" />

        <EditText
            android:id="@+id/password_input"
            android:layout_width="match_parent"
            android:layout_height="45dp"
            android:layout_marginLeft="49dp"
            android:layout_marginRight="49dp"
            app:layout_constraintBottom_toTopOf="parent"
            app:layout_constraintHorizontal_bias="0.491"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toBottomOf="parent"
            app:layout_constraintVertical_bias="0.577" />

        <TextView
            android:id="@+id/login_input_text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:fontFamily="@font/font"
            android:text="E-mail"
            android:textColor="#000"
            android:textSize="20sp"
            app:layout_constraintBottom_toTopOf="@+id/login_input"
            app:layout_constraintHorizontal_bias="0.14"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintVertical_bias="1.0" />

        <TextView
            android:id="@+id/password_input_text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:fontFamily="@font/font"
            android:text="Hasło"
            android:textColor="#000"
            android:textSize="20sp"
            app:layout_constraintBottom_toTopOf="@+id/password_input"
            app:layout_constraintHorizontal_bias="0.136"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintVertical_bias="1.0" />

        <Button
            android:layout_width="match_parent"
            android:layout_height="62dp"
            android:layout_marginLeft="49dp"
            android:layout_marginRight="49dp"
            android:background="@drawable/round_gradient_button"
            android:fontFamily="@font/font"
            android:text="Zaloguj się"
            android:textColor="#000"
            android:textSize="20sp"
            app:layout_constraintBottom_toTopOf="parent"
            app:layout_constraintHorizontal_bias="0.497"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toBottomOf="parent"
            app:layout_constraintVertical_bias="0.341" />

    </androidx.constraintlayout.widget.ConstraintLayout>

</androidx.constraintlayout.widget.ConstraintLayout>

我希望布局完全适合任何屏幕尺寸。

答案

我认为您的约束与xml文件中的视图未正确连接。

尝试一下,我已经修改了您的xml代码。检查一下,让我知道。

<?xml version="1.0" encoding="utf-8"?>
<androidx.core.widget.NestedScrollView
    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">

<androidx.constraintlayout.widget.ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">


    <androidx.constraintlayout.widget.ConstraintLayout
        android:id="@+id/info_box"
        android:layout_width="match_parent"
        android:layout_height="254dp"
        android:background="@color/colorAccent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintBottom_toTopOf="@+id/login_form"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <ImageButton
            android:id="@+id/back_button"
            android:layout_width="45dp"
            android:layout_height="45dp"
            android:layout_marginLeft="8dp"
            android:layout_marginTop="16dp"
            android:background="@drawable/ic_arrow_back_black_24dp"
            android:contentDescription="back_arrow"
            android:tint="#FFFFFF"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintHorizontal_bias="0.005"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintVertical_bias="0.013" />

        <ImageView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginStart="64dp"
            android:layout_marginLeft="64dp"
            android:layout_marginTop="64dp"
            android:layout_marginEnd="64dp"
            android:layout_marginRight="64dp"
            android:layout_marginBottom="64dp"
            android:src="@drawable/ic_launcher_foreground"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.498"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintVertical_bias="0.593" />

    </androidx.constraintlayout.widget.ConstraintLayout>

    <androidx.constraintlayout.widget.ConstraintLayout
        android:id="@+id/login_form"
        android:layout_width="match_parent"
        android:layout_height="478dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintHorizontal_bias="1.0"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/info_box"
        app:layout_constraintVertical_bias="0.0">

        <EditText
            android:id="@+id/login_input"
            android:layout_width="match_parent"
            android:layout_height="45dp"
            android:layout_marginStart="49dp"
            android:layout_marginLeft="49dp"
            android:layout_marginTop="60dp"
            android:layout_marginEnd="49dp"
            android:layout_marginRight="49dp"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <EditText
            android:id="@+id/password_input"
            android:layout_width="match_parent"
            android:layout_height="45dp"
            android:layout_marginStart="49dp"
            android:layout_marginLeft="49dp"
            android:layout_marginTop="44dp"
            android:layout_marginEnd="49dp"
            android:layout_marginRight="49dp"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/login_input" />

        <TextView
            android:id="@+id/login_input_text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="E-mail"
            android:textColor="#000"
            android:textSize="20sp"
            app:layout_constraintBottom_toTopOf="@+id/login_input"
            app:layout_constraintHorizontal_bias="0.14"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintVertical_bias="1.0" />

        <TextView
            android:id="@+id/password_input_text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Hasło"
            android:textColor="#000"
            android:textSize="20sp"
            app:layout_constraintBottom_toTopOf="@+id/password_input"
            app:layout_constraintHorizontal_bias="0.136"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintVertical_bias="1.0" />

        <Button
            android:layout_width="match_parent"
            android:layout_height="62dp"
            android:layout_marginStart="49dp"
            android:layout_marginLeft="49dp"
            android:layout_marginTop="60dp"
            android:layout_marginEnd="49dp"
            android:layout_marginRight="49dp"
            android:text="Zaloguj się"
            android:textColor="#000"
            android:textSize="20sp"
            app:layout_constraintEnd_toEndOf="@+id/password_input"
            app:layout_constraintStart_toStartOf="@+id/password_input"
            app:layout_constraintTop_toBottomOf="@+id/password_input" />

        </androidx.constraintlayout.widget.ConstraintLayout>

    </androidx.constraintlayout.widget.ConstraintLayout>

</androidx.core.widget.NestedScrollView>

快乐编码。

另一答案

考虑使用ScrollView元素,但其中包含ConstraintLayout元素,我建议至少在必要时使用单个ConstraintLayout。刚开始使用ConstraintLayout时,我使用了this,它非常有用。

以上是关于Android studio ConstraintLayout大小调整问题的主要内容,如果未能解决你的问题,请参考以下文章

Android studio 开发一个用户登录界面

android studio 3.1.2 布局文件

Android Studio线性布局设计问题

项目在 Android Studio 模拟器中的位置错误

Android studio gradle同步失败:constraintlayout-solver

Android报错提示:Failed to resolve: com.android.support.constraint:constraint-layout:1.1.3