在Constraint布局中垂直对齐按钮

时间:2018-05-22 12:01:34

标签: android android-constraintlayout

我有这个屏幕,想要将忘记密码下方的登录按钮对齐。忘记密码按钮应与编辑文本字段对齐。然而,事情搞砸了。

layout

这是我的xml代码。

<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"
tools:context=".RegisterActivity">

<include
    layout="@layout/app_bar_layout"
    android:id="@+id/login_toolbar"/>

<android.support.design.widget.TextInputLayout

    android:id="@+id/textInputLayout2"
    android:layout_width="368dp"
    android:layout_height="wrap_content"
    android:layout_marginStart="8dp"
    android:layout_marginTop="200dp"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent">

</android.support.design.widget.TextInputLayout>

<android.support.design.widget.TextInputLayout
    android:id="@+id/textInputLayout3"
    android:layout_width="368dp"
    android:layout_height="wrap_content"
    android:layout_marginStart="8dp"
    android:layout_marginTop="8dp"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/textInputLayout2">

    <android.support.design.widget.TextInputEditText
        android:id="@+id/login_email"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="Email"
        android:inputType="textEmailAddress"

        android:text="@string/email" />
</android.support.design.widget.TextInputLayout>

<android.support.design.widget.TextInputLayout
    android:id="@+id/textInputLayout4"
    android:layout_width="368dp"
    android:layout_height="wrap_content"
    android:layout_marginStart="8dp"
    android:layout_marginTop="8dp"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/textInputLayout3">

    <android.support.design.widget.TextInputEditText
        android:id="@+id/login_password"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="Password"
        android:inputType="textPassword"/>
</android.support.design.widget.TextInputLayout>


<TextView
    android:id="@+id/textView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginBottom="60dp"
    android:text="Login to your Account"
    android:textAllCaps="false"
    android:textAppearance="@style/TextAppearance.AppCompat.Headline"
    app:layout_constraintBottom_toTopOf="@+id/textInputLayout2"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.503"
    app:layout_constraintStart_toStartOf="parent" />

<Button
    android:id="@+id/login_forgot_password_btn"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginStart="116dp"
    android:layout_marginTop="24dp"
    android:background="@color/colorAccent"
    android:padding="16dp"
    android:text="@string/forgot_pass"
    android:textAllCaps="false"
    android:textColor="@android:color/white"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/textInputLayout4" />

<Button
    android:id="@+id/login_create_btn"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="108dp"
    android:background="@color/colorPrimaryDark"
    android:text="@string/login"
    android:textAllCaps="false"
    android:textColor="@android:color/white"
    app:layout_constraintTop_toBottomOf="@+id/textInputLayout4"
    tools:layout_editor_absoluteX="148dp" />

如何对齐两个按钮?

谢谢,

西奥。

1 个答案:

答案 0 :(得分:1)

添加到您的登录btn xml:

android:layout_width="0dp"
app:layout_constraintLeft_toLeftOf="@id/login_forgot_password_btn"
app:layout_constraintRight_toRightOf="@id/login_forgot_password_btn"

和你忘记了通行证:

android:layout_width="0dp"
app:layout_constraintLeft_toLeftOf="@id/textInputLayout4"
app:layout_constraintRight_toRightOf="@id/textInputLayout4"