也许这是一个愚蠢的问题,但我无法找到一种方法来在TextView的第二行末尾插入小图像。图像始终显示在整个文本视图的右侧,而不是行结束。
我想插入这样的图片:
TextTextTextTextTextTextTextTextTextText
TextTextTextText. <ImageView>
我得到的是:
TextTextTextTextTextTextTextTextTextText <ImageView>
TextTextTextText.
我希望有办法做到这一点。
的Src:
<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>
答案 0 :(得分:14)
创建一个ImageSpan并将其添加到textview中。这样,您可以将图像放在文本中所需的位置
示例 -
ImageSpan imagespan = new ImageSpan(appContext, ressource);
text.setSpan(imagespan, i, i+ strLength, 0); //text is an object of TextView
答案 1 :(得分:4)
完成它的一个最佳方法如下......
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));
答案 2 :(得分:1)
如果您想在文本末尾插入 Drawable,Drawable 会隐藏文本的最后一个字符,以避免在文本末尾添加另一个字符并从该字符开始 drawable。
val myText = "Your text"
val span: Spannable = SpannableString(myText+"-")
val android: Drawable = ContextCompat.getDrawable(this, R.drawable.yourDrawable)!!
android.setBounds(0, 0, 30, 30)
val image = ImageSpan(android, ImageSpan.ALIGN_BOTTOM)
span.setSpan(image, span.indexOf("-"), span.indexOf("-")+1, Spannable.SPAN_INCLUSIVE_EXCLUSIVE)
答案 3 :(得分:1)
您可以将图像添加到字符串的末尾,而无需使用如下所示的图像视图;
-rw-r--r--@ 1 dl10skd staff 71369 3 Aug 09:25 artifactory-java-client-api-2.9.2.jar
-rw-r--r--@ 1 dl10skd staff 224679 3 Aug 09:25 artifactory-java-client-services-2.9.2.jar
-rw-r--r-- 1 dl10skd staff 780321 4 Aug 11:29 httpclient-4.5.13.jar
-rw-r--r--@ 1 dl10skd staff 328436 3 Aug 09:25 httpcore-4.4.14.jar
并称之为;
fun addImageToEndOfTheString(text: String, drawableResourceId : Int ,context: Context) : SpannableStringBuilder {
val drawable = ContextCompat.getDrawable(context, drawableResourceId)!!
drawable.setBounds(14, 0, 64, 50)
val rocketImageSpan = ImageSpan(drawable, ImageSpan.ALIGN_BASELINE)
val ssBuilder = SpannableStringBuilder(text)
ssBuilder.setSpan(
rocketImageSpan,
text.length - 1,
text.length,
Spanned.SPAN_EXCLUSIVE_EXCLUSIVE
)
return ssBuilder
}
答案 4 :(得分:0)
这可能不是最佳解决方案,但您可以尝试使用Html.fromHtml将图片插入到自己的行中。
ImageGetter getter = new ImageGetter(){
public Drawable getDrawable(String source){
Drawable d = null;
context.getResources().getDrawable(Integer.parseInt(source));
if (d != null)
d.setBounds(0, 0, d.getIntrinsicWidth(), d.getIntrinsicHeight());
return d;
}
};
String imgString = <source string> + " <img src=\"R.drawable.icon\"/>";
((TextView)findViewById(R.id.tv)).setText(
Html.fromHtml(imgString, getter, null));
答案 5 :(得分:0)
SpannableString ss = new SpannableString(title);
Drawable d = getResources().getDrawable(R.drawable.gallery_list);
d.setBounds(0, 0, d.getIntrinsicWidth(), d.getIntrinsicHeight());
ImageSpan span = new ImageSpan(d, ImageSpan.ALIGN_BASELINE);
ss.setSpan(span, 0, 3, Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
newsTextView.setText(ss);
答案 6 :(得分:0)
TextView textView =new TextView(this);
SpannableStringBuilder ssb = new SpannableStringBuilder( "Here's a smiley how are you " );
Bitmap smiley = BitmapFactory.decodeResource( getResources(), R.drawable.movie_add );
ssb.setSpan( new ImageSpan( smiley ), ssb.length()-1, ssb.length(), Spannable.SPAN_INCLUSIVE_INCLUSIVE );
textView.setText( ssb, BufferType.SPANNABLE );
答案 7 :(得分:0)
感谢Libin Thomas,我为RatingBar
创建了一个解决方案。我将SvgRatingBar
与SVG星星一起使用。
ImageSpan
扩展了DynamicDrawableSpan
,并且DynamicDrawableSpan
只有一个孩子-ImageSpan
。因此,要将另一个View
附加到TextView
的末尾,我们应该从View
得到一个可绘制对象。我向getDrawable()
添加了方法SvgRatingBar
:
public class SvgRatingBar extends AppCompatRatingBar {
...
private Drawable drawable;
public Drawable getDrawable() {
return drawable;
}
private void init() {
LayerDrawable drawable = (LayerDrawable) createTile(getProgressDrawable(), false);
setProgressDrawable(drawable);
this.drawable = drawable;
}
为评分(layout_rating_bar.xml)创建布局:
<?xml version="1.0" encoding="utf-8"?>
<com.example.myapplication.SvgRatingBar xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/rate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="11dp"
android:isIndicator="true"
android:minHeight="13dp"
android:numStars="5"
android:progressDrawable="@drawable/rating_bar"
android:rating="3.5"
android:stepSize="0.01" />
然后编写此代码:
val inflater = getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
val ratingBar: SvgRatingBar = inflater.inflate(R.layout.layout_rating_bar, null, false)
as SvgRatingBar
ratingBar.rating = 3.5f
val drawable = ratingBar.drawable
val ss = SpannableString("dsfjdskljsf dsfjsdkljfslk dsfjlksdjflsdkjf ")
drawable.setBounds(0, 0, 170, 30) // Set width and height of the rating bar here.
val span = ImageSpan(drawable, ImageSpan.ALIGN_BASELINE)
ss.setSpan(span, ss.length - 1, ss.length, Spannable.SPAN_EXCLUSIVE_INCLUSIVE)
textView.setText(ss)
答案 8 :(得分:0)
TextView
类上的 Kotlin 扩展
/**
* Creates a StringSpan using the text from the TextView itself and it also adds an ImageSpan at the
* end of the last line of the TextView.
* This basically allows us to set a drawable at the end of the TextView text.
*/
fun TextView.setImageSpanAtTheEnd(context: Context, @DrawableRes drawableRes: Int) {
text = SpannableString("$text ").apply {
val d = ContextCompat.getDrawable(context, drawableRes) ?: throw RuntimeException("Drawable not found!")
d.setBounds(0, 0, d.intrinsicWidth, d.intrinsicHeight)
setSpan(ImageSpan(d, ImageSpan.ALIGN_BASELINE), text.length, text.length+1, Spannable.SPAN_EXCLUSIVE_INCLUSIVE)
}
}
只需在之后调用它,然后在 TextView
上设置文本即可。