Android 如何在ConstraintLayout中将元素居中

Android 如何在ConstraintLayout中将元素居中,android,android-layout,android-constraintlayout,Android,Android Layout,Android Constraintlayout,我正在应用程序中使用ConstraintLayout来制作应用程序布局。我正在尝试创建一个屏幕,其中一个EditText和按钮应位于中间,而按钮应位于EditText的下方,边距仅为16dp 这是我的布局和截图,它现在看起来如何 活动\u验证\u content.xml <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xml

我正在应用程序中使用
ConstraintLayout
来制作应用程序布局。我正在尝试创建一个屏幕,其中一个
EditText
按钮应位于中间,而
按钮应位于
EditText
的下方,边距仅为16dp

这是我的布局和截图,它现在看起来如何

活动\u验证\u content.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"
    android:paddingLeft="16dp"
    android:paddingRight="16dp"
    tools:context="com.icici.iciciappathon.login.AuthenticationActivity">

    <android.support.design.widget.TextInputLayout
        android:id="@+id/client_id_input_layout"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <android.support.design.widget.TextInputEditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="@string/login_client_id"
            android:inputType="textEmailAddress" />

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

    <android.support.v7.widget.AppCompatButton
        android:id="@+id/authenticate"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginTop="16dp"
        android:text="@string/login_auth"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="@id/client_id_input_layout"
        app:layout_constraintRight_toRightOf="@id/client_id_input_layout"
        app:layout_constraintTop_toTopOf="@id/client_id_input_layout" />

</android.support.constraint.ConstraintLayout>

更新:

您现在可以在
打包
模式中使用
功能,如Eugene的回答中所述


指南

您可以在50%位置使用水平参考线,并向编辑文本和按钮添加底部和顶部(8dp)约束:

<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"
    android:paddingLeft="16dp"
    android:paddingRight="16dp">

    <android.support.design.widget.TextInputLayout
        android:id="@+id/client_id_input_layout"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        app:layout_constraintBottom_toTopOf="@+id/guideline"
        android:layout_marginRight="8dp"
        app:layout_constraintRight_toRightOf="parent"
        android:layout_marginLeft="8dp"
        app:layout_constraintLeft_toLeftOf="parent">

        <android.support.design.widget.TextInputEditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="@string/login_client_id"
            android:inputType="textEmailAddress"/>

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

    <android.support.v7.widget.AppCompatButton
        android:id="@+id/authenticate"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="@string/login_auth"
        app:layout_constraintTop_toTopOf="@+id/guideline"
        android:layout_marginTop="8dp"
        android:layout_marginRight="8dp"
        app:layout_constraintRight_toRightOf="parent"
        android:layout_marginLeft="8dp"
        app:layout_constraintLeft_toLeftOf="parent"/>

    <android.support.constraint.Guideline
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/guideline"
        android:orientation="horizontal"
        app:layout_constraintGuide_percent="0.5"/>

</android.support.constraint.ConstraintLayout>

有一种更简单的方法。如果按以下方式设置布局约束,并且EditText的大小固定,则它将在约束布局中居中:

app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"

左/右对水平居中视图,上/下对垂直居中视图。这是因为,当设置大于视图本身的左、右或上、下约束时,视图将在两个约束之间居中,即偏移设置为50%。您还可以通过自行设置“偏移”来上/下或右/左移动视图。稍微玩一下,你就会看到它是如何影响视图位置的

带指南的解决方案仅适用于单行EditText的这种特殊情况。要使其适用于多行EditText,应使用“打包”链

您可以在以下文章中阅读有关使用链的更多信息:


您可以在ConstraintLayout内部的中心视图中使用layout_constraintCircle

<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:id="@+id/mparent"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <ImageButton
            android:id="@+id/btn_settings"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:srcCompat="@drawable/ic_home_black_24dp"
            app:layout_constraintCircle="@id/mparent"
            app:layout_constraintCircleRadius="0dp"
            />
    </android.support.constraint.ConstraintLayout>


使用constraintCircle to parent和零半径,您可以使视图成为父视图的中心。

您可以按屏幕大小的百分比将视图居中

此示例使用50%的宽度和高度:

<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:background="#FF0000"
        android:orientation="vertical"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintHeight_percent=".5"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintWidth_percent=".5"></LinearLayout>

</android.support.constraint.ConstraintLayout>

在视图中添加这些标记

    app:layout_constraintCircleRadius="0dp"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.5"
    app:layout_constraintStart_toStartOf="parent"
您可以在设计模式中右键单击并选择“中心”。

只需添加

android:gravity="center"
并完成:)

使用

app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
范例

<ImageView
    android:id="@+id/ivIcohn"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/ic_launcher_background"
    android:contentDescription="@string/app_name"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent" />


谢谢@Pycpik我不明白
的用途是什么
布局约束指南的用途是什么
指南
只是一个看不见的项目,你可以在上面锚定你的观点
layout\u constraintGuide\u percent
是父项中的百分比。这里0.5是50%的高度谢谢。明白了。我现在有两个
textinputtext
和一个
按钮
。我想把它们放在屏幕中央。但到目前为止,屏幕上还没有截图。你可以将中间的截图居中,在上面和下面添加一个,或者将它们放在
线性布局中,然后居中。这是一种比使用指南更好的方法。这更合适,我也在许多官员的谈话和示例中看到了这一点。很简单,容易理解。指导方针更好,因为一旦你有了一个复杂的布局,而一旦营销掌握了它,就会发生这种情况,那么简单的中心化就不再有效了。最好有指导方针和顶部和底部的中心指导方针。如果要居中的textview不用于调整其他视图对象的位置,则这非常有用……这当然是最好的答案。其他答案仅适用于一个或两个视图。这一个更具可伸缩性,因为它适用于一个、两个以及任意多个视图。best.solution.ever。就像“app:layout\u constraintCenter\u in=“parent”“(不存在)”一样,此视图不受约束。它只有设计时间位置,因此在运行时它将跳到(0,0),除非您添加您保存的“我的一天”约束,谢谢
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
<ImageView
    android:id="@+id/ivIcohn"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/ic_launcher_background"
    android:contentDescription="@string/app_name"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent" />