如何在多行TextView的末尾插入ImageView?

Sve*_*ver 8 android interface textview imageview

也许这是一个愚蠢的问题,但我无法找到一种方法来在TextView的第二行末尾插入小图像.图像始终显示在整个文本视图的右侧,而不是行结束.

我想插入这样的图像:

TextTextTextTextTextTextTextTextTextText
TextTextTextText. <ImageView>
Run Code Online (Sandbox Code Playgroud)

我得到的是:

TextTextTextTextTextTextTextTextTextText  <ImageView>
TextTextTextText. 
Run Code Online (Sandbox Code Playgroud)

我希望有办法做到这一点.

源:

<RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">

        <TextView
            android:id="@+id/tv"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="TESTESTESTESTESTESTESTESTESTESTESTESTES"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:textColor="#FFFFFF" />

        <ImageView
            android:id="@+id/imageView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_toRightOf="@+id/tv"
            android:src="@drawable/icon" />
    </RelativeLayout>
Run Code Online (Sandbox Code Playgroud)

Bud*_*ril 14

创建一个ImageSpan并将其添加到您的textview.这样,您可以将图像放在文本中所需的位置

示例 -

ImageSpan imagespan = new ImageSpan(appContext, ressource); 
text.setSpan(imagespan, i, i+ strLength, 0); //text is an object of TextView
Run Code Online (Sandbox Code Playgroud)

  • 没有TextView.setSpan方法. (5认同)

小智 5

完成它的一种最佳方法如下...

ImageGetter getter = new ImageGetter(){                 
    public Drawable getDrawable(String source){   // source is the resource name
        Drawable d = null;
        Integer id =  new Integer(0);
        id = getApplicationContext().getResources().getIdentifier(source, "drawable", "com.sampleproject");

        d = getApplicationContext().getResources().getDrawable(id);   

        if (d != null)
            d.setBounds(0, 0, d.getIntrinsicWidth(), d.getIntrinsicHeight());

        return d;
    }
};

String imgString = this.getResources().getString(R.string.text_string) + " <img src=\"img_drawable_image\"/>";
((TextView)findViewById(R.id.textview_id)).setText(
Html.fromHtml(imgString, getter, null));
Run Code Online (Sandbox Code Playgroud)