在此示例中,如何在Textview2之上放置TextView1?

时间:2018-06-22 15:17:35

标签: android layout android-constraintlayout

我需要实现的图像: enter image description here

我有两个FloatingActionButtons,
限制在父项左侧的
另一个在右边,

如何更改以下布局以实现:
拥有2个TextView,它们既占据了按钮之间的所有可用宽度,又占据了可用高度的一半,彼此靠拢?

<?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="wrap_content">


<android.support.design.widget.FloatingActionButton
    android:id="@+id/fab_edit"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    app:backgroundTint="@color/colorPrimary"
    android:layout_marginTop="8dp"
    android:layout_marginBottom="8dp"
    android:layout_marginStart="8dp"
    android:layout_marginEnd="8dp"
    app:fabSize="mini"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:srcCompat="@drawable/ic_edit_w"
    tools:layout_editor_absoluteX="41dp"
    tools:layout_editor_absoluteY="16dp" />

<TextView
    android:id="@+id/txt_top"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:padding="8dp"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintStart_toEndOf="@id/fab_edit"
    android:text="top"/>

<TextView
    android:id="@+id/txt_btm"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:padding="8dp"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toStartOf="@id/fab_play"
    android:text="bottom"/>

<android.support.design.widget.FloatingActionButton
    android:id="@+id/fab_play"
    android:layout_marginTop="8dp"
    android:layout_marginBottom="8dp"
    android:layout_marginStart="8dp"
    android:layout_marginEnd="8dp"
    app:fabSize="mini"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:clickable="true"
    app:srcCompat="@drawable/ic_play_w"
    tools:layout_editor_absoluteX="220dp"
    tools:layout_editor_absoluteY="8dp" />

</android.support.constraint.ConstraintLayout>

1 个答案:

答案 0 :(得分:1)

尝试以下操作:

<?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="wrap_content">


<android.support.design.widget.FloatingActionButton
    android:id="@+id/fab_edit"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    app:backgroundTint="@color/colorPrimary"
    android:layout_marginTop="8dp"
    android:layout_marginBottom="8dp"
    android:layout_marginStart="8dp"
    android:layout_marginEnd="8dp"
    app:fabSize="mini"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    tools:layout_editor_absoluteX="41dp"
    tools:layout_editor_absoluteY="16dp" />

<TextView
    android:id="@+id/txt_top"
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:padding="8dp"
    android:gravity="center"
    app:layout_constraintBottom_toTopOf="@id/txt_btm"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintStart_toEndOf="@id/fab_edit"
    app:layout_constraintEnd_toStartOf="@id/fab_play"
    android:text="top"/>

<TextView
    android:id="@+id/txt_btm"
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:padding="8dp"
    android:gravity="center"
    app:layout_constraintTop_toBottomOf="@id/txt_top"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintStart_toEndOf="@id/fab_edit"
    app:layout_constraintEnd_toStartOf="@id/fab_play"
    android:text="bottom"/>

<android.support.design.widget.FloatingActionButton
    android:id="@+id/fab_play"
    android:layout_marginTop="8dp"
    android:layout_marginBottom="8dp"
    android:layout_marginStart="8dp"
    android:layout_marginEnd="8dp"
    app:fabSize="mini"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:clickable="true"
    tools:layout_editor_absoluteX="220dp"
    tools:layout_editor_absoluteY="8dp" />

</android.support.constraint.ConstraintLayout>