簡體   English   中英

在 Android 中使用 TextView 的可點擊文本 + 圖像

[英]Clickable Text + Image using TextView in Android

如何使用單個TextViewstring之間放置可點擊的image

在此處輸入圖像描述

使用SpannableString ,我可以為字符串的一部分着色。 但是如何添加彩色圖像? 任何建議將不勝感激。

這對於單個TextView元件是不可能的。 TextView並不意味着在它們中間包含圖像。

但是,您可以通過利用drawableTopdrawableBottom等屬性將可繪制資源添加到給定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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM