简体   繁体   English

在 Android 中使用 TextView 的可点击文本 + 图像

[英]Clickable Text + Image using TextView in Android

How to put an clickable image in between string using a single TextView .如何使用单个TextViewstring之间放置可点击的image

在此处输入图像描述

Using SpannableString , I am able to color part of string.使用SpannableString ,我可以为字符串的一部分着色。 But how to add a colored image?但是如何添加彩色图像? Any suggestion would be really appreciated.任何建议将不胜感激。

This is not possible for a single TextView element.这对于单个TextView元件是不可能的。 TextView s are not meant to contain images in the middle of them. TextView并不意味着在它们中间包含图像。

You can, however, add drawable resources to the top, right/start, left/end, or bottom of a given TextView by leveraging the drawableTop , drawableBottom , etc. attributes.但是,您可以通过利用drawableTopdrawableBottom等属性将可绘制资源添加到给定TextView的顶部、右侧/开始、左侧/结束或底部。 But again, no way of adding an image in the middle of a TextView by using only one of them.但同样,仅使用其中一个无法在TextView中间添加图像。

If you really want that specific effect, I would group a few XML components into any ViewGroup like a ConstraintLayout to get exactly what you want;如果您真的想要那种特定的效果,我会将一些 XML 组件分组到任何ViewGroup中,例如ConstraintLayout以获得您想要的效果; eg例如

<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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM