在资源字符串.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>

这可能做到吗?我希望带有其余字符串的图像显示在我的文本视图中。(代码如下(

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

您可以通过像这样使用可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);

很抱歉,图像不可能显示在文本视图中,从可绘制对象调用图像的正确方法是使用 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((方法,但不能将图标用作字符串文本。

最新更新