在ConstraintLayout中对齐按钮

时间:2018-05-03 13:51:27

标签: android xml views

我设计了一个简单的注册屏幕,如下所示。

image1

这是它的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">

<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="8dp"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent">

    <android.support.design.widget.TextInputEditText
        android:id="@+id/reg_display_name"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="hint"
        android:text="Display name" />
</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/reg_email"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="hint"
        android:text="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/reg_password"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="hint"
        android:text="Password" />
</android.support.design.widget.TextInputLayout>

<Button
    android:id="@+id/reg_create_btn"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="64dp"
    android:text="Create account"
    android:textAllCaps="false"
    app:layout_constraintTop_toBottomOf="@+id/textInputLayout4"
    tools:layout_editor_absoluteX="132dp" />

但是,当我运行应用程序时,按钮未在中心对齐。

image2

任何想法如何解决?

谢谢, 西奥。

4 个答案:

答案 0 :(得分:1)

您必须在视图中添加约束,以便将它们置于父布局中

app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"

在每个视图上,您​​的按钮没有这些约束

答案 1 :(得分:1)

您需要添加约束以实现所需的布局,如下所示:

<Button
        android:id="@+id/reg_create_btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="64dp"
        android:text="Create account"
        android:textAllCaps="false"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/textInputLayout4"
        />

答案 2 :(得分:0)

试试此..仅更改按钮。

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

<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="8dp"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent">

    <android.support.design.widget.TextInputEditText
        android:id="@+id/reg_display_name"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="hint"
        android:text="Display name" />
</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/reg_email"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="hint"
        android:text="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/reg_password"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="hint"
        android:text="Password" />
</android.support.design.widget.TextInputLayout>

<Button
    android:id="@+id/reg_create_btn"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="64dp"
    android:text="Create account"
    android:textAllCaps="false"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/textInputLayout4"
    tools:layout_editor_absoluteX="132dp" />
</android.support.constraint.ConstraintLayout>

答案 3 :(得分:0)

由于此属性,您的按钮会在Android Studio预览中居中显示:

const foobar = {
    foo: 'hello',
    bar: undefined
}
foobar.bar = foobar.foo;

但这些tools:layout_editor_absoluteX="132dp" 属性在运行时没有任何影响。要使按钮居中,您应该将其开始和结束约束到父母的开始和结束:

tools