如何使用约束布局创建带有背景色边界框的文本?

How to create a text with a bounding box of a background color using Constrained layout?

我想要实现的是在父级内部使用约束布局来实现以下结果 window:

我在一个大胆的 android 教程中看到,这可以通过使用 ImageView 和 TextView 以及为 TextView 指定的约束来实现 ImageView.But 这是在没有硬编码尺寸的情况下实现的框(即 ImageView 的宽度和高度分别设置为 0dp,并且 ImageView 仅使用约束进行扩展。)

我试过以下但没有给出正确的结果:

   <ImageView
    android:id="@+id/imageViewTable"
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:background="@color/colorPrimary"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toBottomOf="parent"
    android:layout_marginTop="10dp"
    android:layout_marginLeft="16dp"
    android:layout_marginRight="16dp"
    android:layout_marginStart="16dp"
    android:layout_marginEnd="16dp"/>

<TextView
    android:id="@+id/textViewTest"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="TextView"
    android:textAppearance="@style/TextAppearance.AppCompat.Display1"
    app:layout_constraintLeft_toLeftOf="@id/imageViewTable"
    app:layout_constraintRight_toRightOf="@id/imageViewTable"
    app:layout_constraintTop_toTopOf="@id/imageViewTable"
    app:layout_constraintBottom_toBottomOf="@id/imageViewTable"
    />

我知道有一种简单的方法可以使用 padding 来完成,但我想知道如何以这种方式完成(使用 0dp imageview)

扩展 ,您只需按以下方式删除 imageViewTable 并更改 textViewTest

<TextView
    android:id="@+id/textViewTest"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="TextView"
    android:textAppearance="@style/TextAppearance.AppCompat.Display1"
    android:padding="16dp"
    android:background="@color/colorPrimary"
    />
<android.support.constraint.ConstraintLayout xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:background="@color/black"
    android:visibility="visible"
    xmlns:android="http://schemas.android.com/apk/res/android">

    <ImageView
        android:id="@+id/imageViewTable"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:background="@color/colorPrimary"
        app:layout_constraintRight_toRightOf="@+id/textViewTest"
        app:layout_constraintLeft_toLeftOf="@+id/textViewTest"
        app:layout_constraintBottom_toBottomOf="@+id/textViewTest"
        app:layout_constraintTop_toTopOf="@+id/textViewTest" />

    <TextView
        android:padding="16dp"
        android:id="@+id/textViewTest"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextVigjsdgjew"
        android:textColor="@android:color/white"
        style="@style/TextAppearance.AppCompat.Large"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"/>

</android.support.constraint.ConstraintLayout>