最简单的解决方案是使用跨度将黑色文本放置在蓝色文本的 TextView 内。如果黑色文本在 ImageView 内,则将黑色文本 ImageView 限制在最左边的 ImageView 上。当您有不在 ImageView 中的黑色文本时,使 ImageView 的 可见性等于“消失”。
但是让我们假设您有两个 TextViews,其中包含无法组合的文本。
案例 1 - 两个 TextViews
除了最左边的ImageView,还有两个TextView。一个持有蓝色文本,一个持有黑色文本。目标是让这两个 TextViews 看起来像是单个 TextView 的一部分,这样黑色文本就紧跟在蓝色文本之后。
为此,请将黑色 TextView 定位到完全覆盖蓝色 TextView。这可以通过 ConstraintLayout 轻松完成。
activity_main.xml
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/imageView"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_marginStart="16dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@drawable/dandelion_flower" />
<TextView
android:id="@+id/textBlue"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginStart="16dp"
android:layout_marginEnd="16dp"
android:text="Blue text that spane more than one line of its TextView."
android:textColor="@android:color/holo_blue_bright"
app:layout_constrainedWidth="true"
app:layout_constraintBottom_toBottomOf="@id/imageView"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/imageView"
app:layout_constraintTop_toTopOf="@+id/imageView" />
<TextView
android:id="@+id/textBlack"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginStart="16dp"
android:layout_marginEnd="16dp"
android:text="This is some black text"
android:textColor="@android:color/black"
app:layout_constrainedWidth="true"
app:layout_constraintBottom_toBottomOf="@+id/imageView"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/imageView"
app:layout_constraintTop_toTopOf="@+id/imageView" />
</androidx.constraintlayout.widget.ConstraintLayout>
如果不进行调整,黑色文本只会出现在蓝色文本之上。现在的目标是填充蓝色文本 TextView 将有足够的空间,以便它移动到蓝色文本的末尾。我们可以通过使用换行符(将其向下移动)和空格字符(将其向上移动)调整黑色文本来做到这一点。在onCreate() 函数中可以做到这一点。 (参见下面代码中的 cmets。)
// Wait for the layout to complete before getting measurements.
findViewById<TextView>(R.id.textBlue).doOnNextLayout {
val blueText = it as TextView
val blackText = findViewById<TextView>(R.id.textBlack)
// The TextViews have been laid out. Determine how many lines are used for the blue text.
val blueLayout = blueText.layout
val blueLineCount = blueText.layout.lineCount
// And the width in pixels of the last line.
val lastLineWidth = blueLayout.getLineWidth(blueLineCount - 1)
// We will skip over the full lines with newline characters and pad out the last line
// with spaces.
val newLines = "\n".repeat(blueLineCount - 1)
val spaceWidth = blackText.paint.measureText(" ")
val padChars = " ".repeat((lastLineWidth / spaceWidth).toInt() + 1)
blackText.text = "$newLines$padChars${blackText.text}"
}
结果如下:
您可能需要对视图进行一些调整才能完成这项工作,但这是一般的想法。
案例 2 - 一个 TextView 和一个带有文本的 ImageView
在这种情况下,带有文本的 ImageView 被约束到另一个 ImageView 并位于 TextView 下方。具体如何实现取决于您的要求。
两种情况都可以使用一种布局。在情况 #1 中,第二个 ImageView 将其可见性设置为“消失”或“不可见”。在情况 #2 中,黑色文本 TextView 将其可见性设置为“消失”或“不可见”。
我最初认为LeadingMarginSpan 是可行的方法,但如果黑色文本应该从第一行以外的任何行开始,就会遇到困难。
如果您想在 ImageView 周围排列文本,this Stack Overflow 问题和答案应该会有所帮助。