如何在约束布局中修改链长?

时间:2018-05-23 06:47:35

标签: java android android-constraintlayout constraint-layout-chains

这是我的代码

<?xml version="1.0" encoding="utf-8"?>
<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:background="@color/nav_and_action_bar_color"
 >

<ImageView
    android:id="@+id/some_person"
    android:layout_width="100dp"
    android:layout_height="100dp"
    android:src="@drawable/some_person"
    app:layout_constraintBottom_toTopOf="@+id/et_username"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.5"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />


<EditText
    android:id="@+id/et_username"
    android:layout_width="@dimen/login_page_text_fields_width"
    android:layout_height="wrap_content"
    android:layout_margin="10dp"
    android:background="@android:color/white"
    android:hint="@string/email_hint"
    android:padding="10dp"
    app:layout_constraintBottom_toTopOf="@+id/et_password"
    app:layout_constraintHorizontal_bias="0.5"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/some_person" />

<EditText
    android:id="@+id/et_password"
    android:layout_width="@dimen/login_page_text_fields_width"
    android:layout_height="wrap_content"
    android:layout_margin="10dp"
    android:background="@android:color/white"
    android:hint="@string/password_hint"
    android:inputType="textPassword"
    android:padding="10dp"
    app:layout_constraintBottom_toTopOf="@+id/login_button"
    app:layout_constraintHorizontal_bias="0.5"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toBottomOf="@id/et_username" />

<Button
    android:id="@+id/login_button"
    android:layout_width="@dimen/login_button_width"
    android:layout_height="wrap_content"
    android:layout_margin="10dp"
    android:background="@color/colorAccent"
    android:elevation="5dp"
    android:onClick="clickedOnLogin"
    android:text="@string/login_button"
    android:textColor="@android:color/white"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintHorizontal_bias="0.5"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toBottomOf="@id/et_password" />
</android.support.constraint.ConstraintLayout>

我似乎得到了这个结果

emulator screenshot

如何将元素组合在一起?我希望他们能够在一起。

3 个答案:

答案 0 :(得分:1)

您需要为要应用链接逻辑的每个视图定义变量。只选一个:

app:layout_constraintVertical_chainStyle="packed"
app:layout_constraintVertical_chainStyle="spread"
app:layout_constraintVertical_chainStyle="spread_inside"

答案 1 :(得分:1)

使用以下代码获取所需的布局。 以下代码中的更改是“app:layout_constraintVertical_chainStyle =”packed“。

<?xml version="1.0" encoding="utf-8"?>
<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:background="@color/nav_and_action_bar_color">

    <ImageView
        android:id="@+id/some_person"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:src="@drawable/some_person"
        app:layout_constraintBottom_toTopOf="@+id/et_username"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_chainStyle="packed" />


    <EditText
        android:id="@+id/et_username"
        android:layout_width="@dimen/login_page_text_fields_width"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        android:background="@android:color/white"
        android:hint="@string/email_hint"
        android:padding="10dp"
        app:layout_constraintBottom_toTopOf="@+id/et_password"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/some_person" />

    <EditText
        android:id="@+id/et_password"
        android:layout_width="@dimen/login_page_text_fields_width"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        android:background="@android:color/white"
        android:hint="@string/password_hint"
        android:inputType="textPassword"
        android:padding="10dp"
        app:layout_constraintBottom_toTopOf="@+id/login_button"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@id/et_username" />

    <Button
        android:id="@+id/login_button"
        android:layout_width="@dimen/login_button_width"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        android:background="@color/colorAccent"
        android:elevation="5dp"
        android:onClick="clickedOnLogin"
        android:text="@string/login_button"
        android:textColor="@android:color/white"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@id/et_password" />
</android.support.constraint.ConstraintLayout>

答案 2 :(得分:0)

我设法将这些元素紧密结合在一起。根据这个page

  

链条由链条的第一个元素(&#34;头部&#34;)上设置的属性控制:

所以我将其添加到<ImageView>标记

app:layout_constraintVertical_chainStyle="packed"

因为我不希望它垂直居中,所以我也将它添加到同一个标签

app:layout_constraintVertical_bias="0.4"

因此它不在中心,而是偏向顶部。