关于约束布局高度

时间:2020-04-23 18:47:52

标签: android android-studio android-layout

下面的图片是我想要的输出:

enter image description here

我已经经历过

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

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

我所做的是,我已经将约束布局作为主要的父布局,并且在其中做了一个相对的布局,以圆圈为背景,图像位于顶部。

在约束布局的背景中还有一个矩形的顶部弯曲角。现在我没有得到的是这个约束布局的高度,它的底部应该触摸屏幕的底部,但是一旦我将高度声明为match parent,它就会从上到下占据整个屏幕。下面是我的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>

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

enter image description here

如何使其如上图所示?

1 个答案:

答案 0 :(得分:1)

如评论中所述,您无需在 ConstraintLayout 中设置父级布局,因为 ConstraintLayout 功能强大且可以轻松实现你想要什么。您所缺少的是很好地使用了 constraints ,而您正在尝试通过使用 RelativeLayout 对此进行补偿。在 ConstraintLayout 中包含 ConstraintLayout 是一种过大的杀伤力。

我尚未为您 BottomSheet 创建 ResetPassword ,但这是您可以自己进行的工作,因为它只是元素或引入 customView 。我的观点是简单地使用一个 ConstraintLayout 作为将管理所有 ViewGroups 的父级,而不使用任何其他 { {1}}

设计预览

XML布局文件

Layouts