Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/233.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 约束窗口中的对齐按钮_Android_Xml_Views - Fatal编程技术网

Android 约束窗口中的对齐按钮

Android 约束窗口中的对齐按钮,android,xml,views,Android,Xml,Views,我设计了一个简单的注册屏幕,如下所示 下面是它的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:layou

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

下面是它的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" />

有没有办法解决这个问题

谢谢,
Theo.

您必须将约束添加到视图中,以使其在父布局中居中

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

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

您必须将约束添加到视图中,以使其在父布局中居中

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

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

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

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

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

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

试试这个。。仅更改按钮

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

试试这个。。仅更改按钮

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

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

tools:layout_editor_absoluteX="132dp"
但是,这些
工具
属性在运行时没有效果。要使按钮居中,应将其起点和终点约束到父级的起点和终点:

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

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

tools:layout_editor_absoluteX="132dp"
但是,这些
工具
属性在运行时没有效果。要使按钮居中,应将其起点和终点约束到父级的起点和终点:

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

app:layout\u constrainttoop\u toTopOf=“parent”
app:layout\u constraintBottom\u toBottomOf=“parent”
将使按钮浮动在其他视图上