【问题标题】:About the constraint layout height关于约束布局高度
【发布时间】:2020-04-23 18:47:52
【问题描述】:

下图是我想做的输出:

我已经通过了

How to set a view's height match parent in ConstraintLayout?

https://developer.android.com/reference/android/support/constraint/ConstraintLayout

我所做的是我做了一个约束布局作为主要的父布局,在里面,我做了一个相对布局,以圆圈为背景,顶部为图像。

还有一个矩形顶部弯曲角位于约束布局的背景中。现在我没有得到的是这个约束布局的高度,它的底部应该触及屏幕的底部,但是一旦我将高度声明为匹配父级,它就会从上到下占据整个屏幕。下面是我的 XML 代码。

     <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:background="@color/silver"
tools:context=".ResetPassword">


<RelativeLayout
    android:layout_width="40dp"
    android:layout_height="40dp"
    android:layout_marginLeft="20dp"
    android:layout_marginStart="20dp"
    android:layout_marginTop="20dp"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    android:background="@drawable/circle_shape"
    >
    <ImageView
        android:id="@+id/iv_reset_bck"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:src="@drawable/ic_arrow_back_black_24dp"></ImageView>

</RelativeLayout>


<RelativeLayout
    android:id="@+id/rl_reset_topImage"
    android:layout_width="200dp"
    android:layout_height="200dp"
    android:layout_marginTop="40dp"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.498"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    android:background="@drawable/circle_shape">
    <ImageView
        android:id="@+id/iv_reset"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_alignParentBottom="true"
        android:layout_marginBottom="20dp"
        app:srcCompat="@mipmap/ic_launcher_round" />
</RelativeLayout>
    <androidx.constraintlayout.widget.ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="20dp"
    android:background="@drawable/bg_rectangle"
    android:paddingLeft="5dp"
    android:paddingTop="5dp"
    android:paddingRight="5dp"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/rl_reset_topImage">

    <LinearLayout
        android:id="@+id/welcomeLoginLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        tools:layout_editor_absoluteY="5dp">

        <com.example.portfolio.CustomViews.PopinsBoldTextView
            android:id="@+id/tv_welcome"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="43dp"
            android:layout_marginLeft="43dp"
            android:layout_marginTop="44dp"
            android:text="@string/welcome"
            android:textColor="@color/black"
            android:textSize="26sp"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <com.example.portfolio.CustomViews.PopinsBoldTextView
            android:id="@+id/tv_logintext_msg"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="43dp"
            android:layout_marginLeft="43dp"
            android:text="@string/loginwith"
            android:textColor="@color/black"
            android:textSize="20sp"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />
    </LinearLayout>

    <RelativeLayout
        android:id="@+id/emailRelativeLayout"
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:layout_marginStart="40dp"
        android:layout_marginLeft="40dp"
        android:layout_marginTop="20dp"
        android:layout_marginEnd="40dp"
        android:layout_marginRight="40dp"
        android:background="@drawable/bg_email_rect"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/welcomeLoginLayout">

        <EditText
            android:id="@+id/et_email"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginStart="20dp"
            android:layout_marginLeft="20dp"
            android:layout_marginEnd="20dp"
            android:layout_marginRight="20dp"
            android:background="@android:color/transparent"
            android:hint="@string/prompt_email"
            android:paddingTop="5dp"
            android:textSize="20sp"
            android:paddingBottom="5dp"
            android:textColorHint="@color/dark_gey"></EditText>
    </RelativeLayout>

    <RelativeLayout
        android:id="@+id/passwordlRelativeLayout"
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:layout_marginStart="40dp"
        android:layout_marginLeft="40dp"
        android:layout_marginTop="20dp"
        android:layout_marginEnd="40dp"
        android:layout_marginRight="40dp"
        android:background="@drawable/bg_email_rect"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/emailRelativeLayout">

        <EditText
            android:id="@+id/et_password"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginStart="20dp"
            android:layout_marginLeft="20dp"
            android:layout_marginEnd="20dp"
            android:layout_marginRight="20dp"
            android:background="@android:color/transparent"
            android:hint="@string/prompt_password"
            android:textSize="20sp"
            android:paddingTop="5dp"
            android:paddingBottom="5dp"
            android:textColorHint="@color/dark_gey"></EditText>
    </RelativeLayout>

    <RelativeLayout
        android:id="@+id/login_btnLayout"
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:layout_marginStart="40dp"
        android:layout_marginLeft="40dp"
        android:layout_marginTop="20dp"
        android:layout_marginEnd="40dp"
        android:layout_marginRight="40dp"
        android:background="@drawable/bg_btn"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/passwordlRelativeLayout">


        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:text="Login"
            android:textColor="#FFF"
            android:textSize="20dp"
            android:textStyle="bold"></TextView>
    </RelativeLayout>
 </androidx.constraintlayout.widget.ConstraintLayout>
</androidx.constraintlayout.widget.ConstraintLayout>

这就是我从上面的代码得到的输出

如何才能如上图那样?

【问题讨论】:

  • ConstrantLayout 内不需要有任何其他的父 Layout。只需使用约束,您就可以在没有任何其他布局的情况下实现最终结果。

标签: android android-studio android-layout


【解决方案1】:

正如评论中提到的,您不需要在 ConstraintLayout 中设置父布局,因为 ConstraintLayout 非常强大,可以轻松实现您想要的。您缺少的是很好地使用 constraints,而您正试图通过使用 RelativeLayout 来弥补这一点。在 ConstraintLayout 中包含 ConstraintLayout 有点过头了。

我还没有为你创建 BottomSheet ResetPassword,但你可以自己处理它,因为它只是元素的排列或引入customView。我的意思是简单地使用一个 ConstraintLayout 作为将管理所有 ViewGroups 的父级,并避免使用任何其他 Layouts strong> 在里面。

设计预览

XML 布局文件

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

  <ImageView
    android:id="@+id/imageView2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="0.25"
    tools:srcCompat="@tools:sample/avatars" />

  <TextView
    android:id="@+id/textView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="100dp"
    android:text="Reset your password"
    app:layout_constraintEnd_toEndOf="@+id/imageView2"
    app:layout_constraintStart_toStartOf="@+id/imageView2"
    app:layout_constraintTop_toBottomOf="@+id/imageView2" />

  <TextView
    android:id="@+id/textView2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="16dp"
    android:text="Reset your password to use app"
    app:layout_constraintEnd_toEndOf="@+id/textView"
    app:layout_constraintStart_toStartOf="@+id/textView"
    app:layout_constraintTop_toBottomOf="@+id/textView" />

  <EditText
    android:id="@+id/editTextTextPersonName"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="100dp"
    android:ems="10"
    android:inputType="textPersonName"
    android:text="Name"
    app:layout_constraintEnd_toEndOf="@+id/textView2"
    app:layout_constraintStart_toStartOf="@+id/textView2"
    app:layout_constraintTop_toBottomOf="@+id/textView2" />

  <EditText
    android:id="@+id/editTextTextPassword"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:ems="10"
    android:hint="Password"
    android:inputType="textPassword"
    app:layout_constraintEnd_toEndOf="@+id/editTextTextPersonName"
    app:layout_constraintStart_toStartOf="@+id/editTextTextPersonName"
    app:layout_constraintTop_toBottomOf="@+id/editTextTextPersonName" />

  <Button
    android:id="@+id/button"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:text="Button"
    app:layout_constraintEnd_toEndOf="@+id/editTextTextPassword"
    app:layout_constraintStart_toStartOf="@+id/editTextTextPassword"
    app:layout_constraintTop_toBottomOf="@+id/editTextTextPassword" />

</androidx.constraintlayout.widget.ConstraintLayout>

【讨论】:

  • 感谢您努力回答这个问题,但问题是您在代码中所做的是您已包含“硬编码”边距 top=100dp 属性,这不会适合所有尺寸的屏幕,与输出图像中显示的相同。因为我们是开发人员,所以我们必须首先坚持设计所以我很抱歉,但这并不能解决我的问题我想要的是弯曲布局的底部应该接触到屏幕的底部。我知道它只是使用约束,如果你仍然可以欺骗它,那将是可观的。
  • 您可以简单地将所有这些硬编码的值放入 assets 文件夹中。更好的是,创建自定义样式并将其应用到您想要的任何地方。我的意思是告诉你你使用了错误的约束布局。您有多个布局层次结构,这会使您感到困惑并使您的 UI 在旧设备上变慢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-10-12
  • 1970-01-01
  • 1970-01-01
  • 2018-04-17
  • 2020-06-05
  • 2021-08-12
相关资源
最近更新 更多