[英]Constrain TextView in two views at the same time
最简单的解决方案是使用跨度将黑色文本放置在蓝色文本的TextView中。 如果黑色文本在ImageView内,则将黑色文本ImageView限制在最左边的ImageView 。 当您有不在ImageView中的黑色文本时,使ImageView 的可见性等于“消失”。
但是让我们假设您有两个TextView,其中的文本无法组合。
案例 1 - 两个TextView
除了最左边的ImageView 之外,还有两个TextViews 。 一个包含蓝色文本,一个包含黑色文本。 目标是使这两个TextView看起来像是单个TextView 的一部分,以便黑色文本紧跟在蓝色文本之后。
为此,将黑色TextView定位为完全覆盖蓝色TextView 。 这可以通过ConstraintLayout轻松完成。
活动_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()
函数中。 (请参阅以下代码中的注释。)
// 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周围流动文本, 这个Stack Overflow 问题和答案应该会有所帮助。
嗨,尝试将 flexWrap 属性设置为 wrap 的Google 的 FlexboxLayout
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.