【问题标题】:How to create a text with a bounding box of a background color using Constrained layout?如何使用约束布局创建带有背景颜色边界框的文本?
【发布时间】:2017-04-05 20:54:59
【问题描述】:

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

我在一个 udacity android 教程中看到,这可以通过使用 ImageView 和 TextView 来实现,其中 TextView wrt 到 ImageView 指定了约束。但是这是在没有硬编码框的尺寸(即宽度和高度)的情况下实现的。 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)

【问题讨论】:

  • 你为什么不做一个带有填充和绿色背景的文本视图?
  • @Tiago 我知道可以这样做。但我想知道使用 0dp ImageView 的“技巧”。我已编辑问题以反映这一点。

标签: android android-layout view android-constraintlayout


【解决方案1】:
<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>

【讨论】:

  • 感谢您的回答。但我认为在这种情况下,ImageView 的尺寸是固定的,不依赖于 TextView 的大小。所以它可能不符合我的目的。我找到了方法。我稍后将更新答案。
  • @banguru,很抱歉我第一次没能得到你的问题。我已经更新了我的答案。
  • 是的,你是对的。我认为你需要删除 0dp 边距(android:layout_marginTop="0dp" 等),否则视图会拉伸整个窗口!请更正它,我会接受作为答案!
【解决方案2】:

扩展Tiago's answer,您只需删除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"
    />

【讨论】:

  • 我知道可以这样做。但我想知道使用 0dp ImageView 的“技巧”。我已编辑问题以反映这一点。
猜你喜欢
  • 2016-03-18
  • 1970-01-01
  • 2018-04-09
  • 2013-03-09
  • 2021-02-24
  • 1970-01-01
  • 1970-01-01
  • 2015-05-22
  • 1970-01-01
相关资源
最近更新 更多