在多行文本视图的开头添加可绘制对象

3
需要将引用作为可绘制对象添加到多行文本中,就像下面提到的屏幕一样。就像您可以看到的那样,引用位于多行文本的开头,下一行文本刚好从引用下方开始。

Quote(drawable) should be starting of text in Textview and enter image description here

我需要使用TextView来实现这个功能,引号应该是一个可绘制的对象。
如果我使用TextView的drawableLeft属性,它会显示如下图所示。

enter image description here

     <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!\nHello World!\nHello World!\nHello World!"
        android:drawableStart="@drawable/ic_quote"
        android:drawableLeft="@drawable/ic_quote"/>

这个链接可能对你有帮助:https://dev59.com/s18e5IYBdhLWcg3wlbFR - undefined
这不是我需要的。如果你仔细看图片,下一行的文本应该从drawable的起始位置开始。所有这些都需要使用一个带有多行文本的TextView来实现。 - undefined
3个回答

4
你可以创建一个SpannableString来添加可绘制对象:
TextView myTextView = (TextView) findViewById(R.id.myTextView);

ImageSpan imageSpan = new ImageSpan(this, R.drawable.ic_quote);
SpannableString spannableString = new SpannableString("*" + myTextView.getText()); // "*" will be replaced by your drawable

int start = 0; // the start index, inclusive
int end = 1; // the end index, exclusive
int flag = 0;
spannableString.setSpan(imageSpan, start, end, flag);

myTextView.setText(spannableString);

在你的布局中:

<TextView
   android:id="@+id/myTextView"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:text="Hello World!\nHello World!\nHello World!\nHello World!"/>

完美,我的朋友。 - undefined

0
尝试使用 android:gravity="top"
如果这样不起作用,那就使用负边距,像这样。
android:drawablePadding="-20sp"

另一种替代方法是在 LinearLayout 内部将 TextView 旁边放置一个 ImageView,这样你可以应用重力效果


这不是我所需要的。如果你正确地看到了图片。下一行文字应该从最开始的位置开始,并且同时在drawable的下方。所有这些都需要通过一个具有多行文本的textview来实现。 - undefined

0
你可以使用SpannableStringBuilder。
 val spannableText = SpannableStringBuilder("*Your text!").apply {
            val imageSpan = ImageSpan(context, R.drawable.your_icon)
            setSpan(imageSpan, 0, 1, Spannable.SPAN_INCLUSIVE_INCLUSIVE)
 }
 textView.setText(spannableText, TextView.BufferType.SPANNABLE)

符号 * 将被替换为您的图标。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接