View 在 ConstraintLayout 中打包,Views 运行 在 LinearLayout 中超出屏幕

View get packed in ConstraintLayout, Views run out of screen in LinearLayout

我正在实现如下所示的 signup/login 屏幕

我已经尝试使用 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"
    android:layout_margin="16dp"
    tools:context=".MainActivity">

    <ImageView
        android:id="@+id/img"
        android:layout_width="250dp"
        android:layout_height="250dp"
        android:layout_marginBottom="72dp"
        android:src="@drawable/ic_launcher_background"
        app:layout_constraintBottom_toTopOf="@+id/btn1"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_chainStyle="packed" />

    <Button
        android:id="@+id/btn1"
        android:layout_width="0dp"
        android:layout_height="56dp"
        android:layout_marginStart="32dp"
        android:layout_marginEnd="32dp"
        android:layout_marginBottom="12dp"
        android:text="Button 1"
        app:layout_constraintBottom_toTopOf="@+id/btn2"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/img" />

    <Button
        android:id="@+id/btn2"
        android:layout_width="0dp"
        android:layout_height="56dp"
        android:layout_marginBottom="48dp"
        android:text="Button 2"
        app:layout_constraintBottom_toTopOf="@+id/txt_already_member"
        app:layout_constraintEnd_toEndOf="@+id/btn1"
        app:layout_constraintStart_toStartOf="@+id/btn1"
        app:layout_constraintTop_toBottomOf="@+id/btn1" />

    <TextView
        android:id="@+id/txt_already_member"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Already Member? Sign In"
        android:textSize="16sp"
        app:layout_constraintBottom_toTopOf="@+id/txt_agreement"
        app:layout_constraintEnd_toEndOf="@+id/btn2"
        app:layout_constraintStart_toStartOf="@+id/btn2"
        app:layout_constraintTop_toBottomOf="@+id/btn2" />

    <TextView
        android:id="@+id/txt_agreement"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="By continuing you agree to our terms and privacy policy"
        android:layout_marginTop="32dp"
        android:textSize="12sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

XML 摘要:

问题是当我 运行 在我的 phone 上显示时,底部文本视图变得紧凑,如图 here。

接下来我尝试 LinearLayout 如下所示

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:gravity="center_horizontal"
    android:layout_margin="16dp"
    android:layout_height="match_parent">

    <ImageView
        android:id="@+id/img"
        android:layout_width="250dp"
        android:layout_height="250dp"
        android:layout_marginBottom="72dp"
        android:layout_marginTop="72dp"
        android:src="@drawable/ic_launcher_background"/>

    <Button
        android:id="@+id/btn1"
        android:layout_width="match_parent"
        android:layout_height="56dp"
        android:layout_marginStart="32dp"
        android:layout_marginEnd="32dp"
        android:layout_marginBottom="8dp"
        android:text="Button 1" />

    <Button
        android:id="@+id/btn2"
        android:layout_width="match_parent"
        android:layout_marginStart="32dp"
        android:layout_marginEnd="32dp"
        android:layout_height="56dp"
        android:layout_marginBottom="32dp"
        android:text="Button 2" />

    <TextView
        android:id="@+id/txt_already_member"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Already Member? Sign In"
        android:textSize="16sp"
        android:layout_weight="1"/>

    <TextView
        android:id="@+id/txt_agreement"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="By continuing you agree to our terms and privacy policy "
        android:textSize="12sp"/>
</LinearLayout>

在这种情况下,底部文本视图不可见,如图所示 here。

我的设备分辨率是 720 * 1280 像素。

我想问题出在硬边距上。无论屏幕高度如何,我怎样才能让屏幕看起来像上面的草图?使用 LinearLayoutConstraintLayout?

我建议您使用所有屏幕密度而不是一个,为此您可以使用著名的库 SDP or follow the guideline provided by android。 第二次使用 Guideline class 进行 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"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="16dp">

<ImageView
    android:id="@+id/img"
    android:layout_width="@dimen/_200sdp"
    android:layout_height="@dimen/_200sdp"
    android:layout_marginTop="16dp"
    android:src="@drawable/ic_launcher"
    app:layout_constraintBottom_toTopOf="@+id/btn1"
    app:layout_constraintEnd_toStartOf="@+id/right"
    app:layout_constraintHorizontal_bias="0.5"
    app:layout_constraintStart_toStartOf="@+id/left"
    app:layout_constraintTop_toTopOf="parent" />

<Button
    android:id="@+id/btn1"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:text="Button 1"
    app:layout_constraintBottom_toTopOf="@+id/btn2"
    app:layout_constraintEnd_toStartOf="@+id/right"
    app:layout_constraintHorizontal_bias="0.5"
    app:layout_constraintStart_toStartOf="@+id/left"
    app:layout_constraintTop_toBottomOf="@+id/img" />

<Button
    android:id="@+id/btn2"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:text="Button 2"
    app:layout_constraintBottom_toTopOf="@+id/txt_already_member"
    app:layout_constraintEnd_toStartOf="@+id/right"
    app:layout_constraintHorizontal_bias="0.5"
    app:layout_constraintStart_toStartOf="@+id/left"
    app:layout_constraintTop_toBottomOf="@+id/btn1" />

<TextView
    android:id="@+id/txt_already_member"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="20dp"
    android:text="Already Member? Sign In"
    android:textSize="16sp"
    app:layout_constraintBottom_toTopOf="@+id/divider"
    app:layout_constraintEnd_toStartOf="@+id/right"
    app:layout_constraintHorizontal_bias="0.5"
    app:layout_constraintStart_toStartOf="@+id/left"
    app:layout_constraintTop_toBottomOf="@+id/btn2" />

<TextView
    android:id="@+id/txt_agreement"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginBottom="24dp"
    android:text="By continuing you agree to our terms and privacy policy"
    android:textSize="12sp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toStartOf="@+id/right"
    app:layout_constraintStart_toStartOf="@+id/left"
    app:layout_constraintTop_toTopOf="@+id/divider"
    app:layout_constraintVertical_bias="1.0" />

<androidx.constraintlayout.widget.Guideline
    android:id="@+id/left"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    app:layout_constraintGuide_begin="@dimen/_16sdp" />

<androidx.constraintlayout.widget.Guideline
    android:id="@+id/right"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    app:layout_constraintGuide_end="@dimen/_16sdp" />

<androidx.constraintlayout.widget.Guideline
    android:id="@+id/divider"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintGuide_percent="0.7"
    app:layout_constraintTop_toBottomOf="@+id/txt_already_member" />

实际上我的问题的解决方案是在 ConstraintLayout 中使用 Guidelines 和 Guidelines app:layout_constraintGuide_percent 属性并将视图限制为指南。

由于 app:layout_constraintGuide_percent 的值是 百分比 而不是 dp 值,因此无论屏幕 width/height.

示例:

<?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"
    android:layout_margin="16dp"
    tools:context=".MainActivity">

    <ImageView
        android:id="@+id/img"
        android:layout_width="250dp"
        android:layout_height="200dp"
        android:src="@drawable/ic_launcher_background"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="@+id/guideline4" />

    <Button
        android:id="@+id/btn1"
        android:layout_width="match_parent"
        android:layout_height="56dp"
        android:layout_marginStart="20dp"
        android:layout_marginEnd="20dp"
        android:text="Button 1"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="1.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="@+id/guideline5" />

    <Button
        android:id="@+id/btn2"
        android:layout_width="match_parent"
        android:layout_height="56dp"
        android:layout_marginTop="8dp"
        android:layout_marginStart="20dp"
        android:layout_marginEnd="20dp"
        android:text="Button 2"
        app:layout_constraintEnd_toEndOf="@+id/btn1"
        app:layout_constraintStart_toStartOf="@+id/btn1"
        app:layout_constraintTop_toBottomOf="@+id/btn1" />

    <TextView
        android:id="@+id/txt_already_member"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="16dp"
        android:layout_marginBottom="16dp"
        android:text="Already Member? Sign In"
        android:textSize="16sp"
        app:layout_constraintBottom_toTopOf="@+id/txt_agreement"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/btn2" />

    <TextView
        android:id="@+id/txt_agreement"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="32dp"
        android:text="By continuing you agree to our terms and privacy policy "
        android:textSize="12sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent" />

    <androidx.constraintlayout.widget.Guideline
        android:id="@+id/guideline4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        app:layout_constraintGuide_percent=".1" />

    <androidx.constraintlayout.widget.Guideline
        android:id="@+id/guideline5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        app:layout_constraintGuide_percent=".6" />


</androidx.constraintlayout.widget.ConstraintLayout>

非常感谢 Rehan Sarwar 记得我 Guidelines。