Android约束布局:如何将视图与另一个视图垂直对齐

时间:2019-05-06 04:49:09

标签: android android-constraintlayout

我希望“编辑”文本视图与PROFILE垂直对齐。 我尝试了很多有意义的事情。

使用这些约束

<?xml version="1.0" encoding="utf-8"?>
    <androidx.constraintlayout.widget.ConstraintLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:id="@+id/parentLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/colorPrimary">
        <TextView
            android:id="@+id/titleTxt"
            android:layout_width="wrap_content"
            android:text="PROFILE"
            android:layout_height="wrap_content"
            android:textColor="@android:color/white"
            android:textSize="25sp"
            android:textStyle="bold"
            android:layout_marginTop="24dp"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
        />

        <TextView
            android:id="@+id/editBtn"
            android:text="Edit"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginEnd="16dp"
            android:layout_marginTop="18dp"
            android:textSize="20sp"
            android:textColor="@android:color/white"
            android:padding="8dp"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintTop_toTopOf="titleTxt"
            app:layout_constraintBottom_toBottomOf="titleTxt"
        />
    </androidx.constraintlayout.widget.ConstraintLayout>

这是我当前的输出

enter image description here

4 个答案:

答案 0 :(得分:1)

您必须像下面那样对齐编辑TextView

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:id="@+id/parentLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/colorPrimary">
    <TextView
            android:id="@+id/titleTxt"
            android:layout_width="wrap_content"
            android:text="PROFILE"
            android:layout_height="wrap_content"
            android:textColor="@android:color/white"
            android:textSize="25sp"
            android:textStyle="bold"
            android:layout_marginTop="24dp"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
    />

    <TextView
            android:id="@+id/editBtn"
            android:text="Edit"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginEnd="16dp"
            android:textSize="20sp"
            android:textColor="@android:color/white"
            android:padding="8dp"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintTop_toTopOf="@+id/titleTxt"
            app:layout_constraintBottom_toBottomOf="@+id/titleTxt"
    />
</androidx.constraintlayout.widget.ConstraintLayout>

输出

enter image description here

答案 1 :(得分:0)

您可以尝试-

<?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/colorPrimary"
    tools:context=".activity.WebviewActivity">

    <TextView
        android:id="@+id/titleTxt"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="24dp"
        android:text="PROFILE"
        android:textColor="@android:color/white"
        android:textSize="25sp"
        android:textStyle="bold"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/editBtn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginTop="18dp"
        android:layout_marginEnd="16dp"
        android:padding="8dp"
        android:text="Edit"
        android:textColor="@android:color/white"
        android:textSize="20sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</android.support.constraint.ConstraintLayout>

答案 2 :(得分:0)

您需要将基线从编辑Textview 设置为个人资料Textview

app:layout_constraintBaseline_toBaselineOf =“ @ + id / titleTxt”

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/parentLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/colorPrimary">
    <TextView
        android:id="@+id/titleTxt"
        android:layout_width="wrap_content"
        android:text="PROFILE"
        android:layout_height="wrap_content"
        android:textColor="@android:color/white"
        android:textSize="25sp"
        android:textStyle="bold"
        android:layout_marginTop="24dp"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        />

    <TextView
        android:id="@+id/editBtn"
        android:layout_width="wrap_content"
        android:layout_height="42dp"
        android:layout_marginEnd="16dp"
        android:padding="8dp"
        android:text="Edit"
        android:textColor="@android:color/white"
        android:textSize="20sp"
        app:layout_constraintBaseline_toBaselineOf="@+id/titleTxt"
        app:layout_constraintEnd_toEndOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

答案 3 :(得分:0)

尝试一下

lib/modules