簡體   English   中英

在資源字符串中添加drawable。xml

[英]Add drawable in resources string.xml

我正在嘗試在資源字符串中的字符串(my_string_name)中添加可繪制對象(ic_myimage)。xml

<resources>
    <string name="my_string_name">Here is my image: @drawable/ic_myimage It looks great</string>
</resources>

這可能嗎? 我希望字符串的 rest 的圖像顯示在我的 TextView 中。 (代碼如下)

<TextView
        android:id="@+id/myTextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/my_string_name"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

注意:我嘗試了在另一篇文章中找到的以下內容,但沒有奏效

<string name="my_string_name">Here is my image: [img src=ic_myimage/] It looks great</string>

我正在使用 Android Studio 和 Kotlin。

很抱歉,但不可能在 Textview 中顯示圖像,從 drawable 調用圖像的正確方法是使用 ImageView。

像這個。

<ImageView
            android:id="@+id/imageView"
            android:layout_width="match_parent"
            android:layout_gravity="center"
            android:layout_height="200dp"
            android:layout_marginStart="30dp"
            android:layout_marginTop="8dp"
            android:layout_marginEnd="25dp"
            android:scaleType="centerInside"
            android:layout_marginLeft="100dp"
            android:layout_marginRight="8dp"
            android:background="@drawable/ic_myimage">

</ImageView>

請使用在文本視圖左側顯示圖標的文本視圖的 drawableStart() 方法。但您不能將圖標用作字符串文本。

您可以通過使用 Spannable 字符串提供文本來做到這一點

//your drawable    
ImageSpan is = new ImageSpan(context, R.drawable.arrow);

//your spannable text "Lorem.... amet"
SpannableString spannableText= new SpannableString("Lorem ipsum dolor sit amet");

//apply image to spannable text 
//0 is the flag
//start and end are the index length where you wantg to put the image
spannableText.setSpan(is, startIndex, End index, 0);

//then, you can  apply this tspannable text to textview
yourTextView.setText(spannableText);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

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