繁体   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