[英]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.