[英]Clickable Text + Image using TextView in Android
这对于单个TextView
元件是不可能的。 TextView
并不意味着在它们中间包含图像。
但是,您可以通过利用drawableTop
、 drawableBottom
等属性将可绘制资源添加到给定TextView
的顶部、右侧/开始、左侧/结束或底部。 但同样,仅使用其中一个无法在TextView
中间添加图像。
如果您真的想要那种特定的效果,我会将一些 XML 组件分组到任何ViewGroup
中,例如ConstraintLayout
以获得您想要的效果; 例如
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:id="@+id/text_part_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="first part of the text"
android:textSize="16dp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@id/image"/>
<ImageView
android:id="@+id/image"
android:layout_width="50dp"
android:layout_height="50dp"
android:background="@color/white"
app:layout_constraintStart_toStartOf="@id/text_part_1"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintEnd_toEndOf="@id/text_part_2"/>
<TextView
android:id="@+id/text_part_2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="first part of the text"
android:textSize="16dp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toEndOf="@id/image"/>
</androidx.constraintlayout.widget.ConstraintLayout>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.