我在Google上搜索并浏览了这个网站,在那里我发现了一个类似于我的问题,其中如何在TextView
文本中包含图像,例如“你好我的名字是[图片]“,答案是这样的:
ImageSpan is = new ImageSpan(context, resId);
text.setSpan(is, index, index + strLength, 0);
我想在此代码中知道,
text.setSpan()
做一些事情,比如导入或引用还是留下文字?如果有人能为我打破这一点,我将非常感激。
答案 0 :(得分:179)
试试这个..
txtview.setCompoundDrawablesWithIntrinsicBounds(
R.drawable.image, 0, 0, 0);
另见.. http://developer.android.com/reference/android/widget/TextView.html
在xml文件中尝试此操作
<TextView
android:id="@+id/txtStatus"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:drawableLeft="@drawable/image"
android:drawablePadding="5dp"
android:singleLine="true"
android:text="@string/name"/>
答案 1 :(得分:68)
com / xyz / customandroid / TextViewWithImages .java :
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import android.content.Context;
import android.text.Spannable;
import android.text.style.ImageSpan;
import android.util.AttributeSet;
import android.util.Log;
import android.widget.TextView;
public class TextViewWithImages extends TextView {
public TextViewWithImages(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public TextViewWithImages(Context context, AttributeSet attrs) {
super(context, attrs);
}
public TextViewWithImages(Context context) {
super(context);
}
@Override
public void setText(CharSequence text, BufferType type) {
Spannable s = getTextWithImages(getContext(), text);
super.setText(s, BufferType.SPANNABLE);
}
private static final Spannable.Factory spannableFactory = Spannable.Factory.getInstance();
private static boolean addImages(Context context, Spannable spannable) {
Pattern refImg = Pattern.compile("\\Q[img src=\\E([a-zA-Z0-9_]+?)\\Q/]\\E");
boolean hasChanges = false;
Matcher matcher = refImg.matcher(spannable);
while (matcher.find()) {
boolean set = true;
for (ImageSpan span : spannable.getSpans(matcher.start(), matcher.end(), ImageSpan.class)) {
if (spannable.getSpanStart(span) >= matcher.start()
&& spannable.getSpanEnd(span) <= matcher.end()
) {
spannable.removeSpan(span);
} else {
set = false;
break;
}
}
String resname = spannable.subSequence(matcher.start(1), matcher.end(1)).toString().trim();
int id = context.getResources().getIdentifier(resname, "drawable", context.getPackageName());
if (set) {
hasChanges = true;
spannable.setSpan( new ImageSpan(context, id),
matcher.start(),
matcher.end(),
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE
);
}
}
return hasChanges;
}
private static Spannable getTextWithImages(Context context, CharSequence text) {
Spannable spannable = spannableFactory.newSpannable(text);
addImages(context, spannable);
return spannable;
}
}
使用:强>
res / layout / mylayout.xml 中的:
<com.xyz.customandroid.TextViewWithImages
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#FFFFFF00"
android:text="@string/can_try_again"
android:textSize="12dip"
style=...
/>
请注意,如果将 TextViewWithImages.java 放在 com / xyz / customandroid / 以外的某个位置,则还必须更改包名称com.xyz.customandroid
上方。
:
<string name="can_try_again">Press [img src=ok16/] to accept or [img src=retry16/] to retry</string>
其中 ok16.png 和 retry16.png 是 res / drawable / 文件夹中的图标
答案 2 :(得分:10)
我尝试了很多不同的解决方案,这对我来说是最好的:
SpannableStringBuilder ssb = new SpannableStringBuilder(" Hello world!");
ssb.setSpan(new ImageSpan(context, R.drawable.image), 0, 1, Spannable.SPAN_INCLUSIVE_INCLUSIVE);
tv_text.setText(ssb, TextView.BufferType.SPANNABLE);
此代码使用最少的内存。
答案 3 :(得分:6)
fun TextView.addImage(atText: String, @DrawableRes imgSrc: Int, imgWidth: Int, imgHeight: Int) {
val ssb = SpannableStringBuilder(this.text)
val drawable = ContextCompat.getDrawable(this.context, imgSrc) ?: return
drawable.mutate()
drawable.setBounds(0, 0,
imgWidth,
imgHeight)
val start = text.indexOf(atText)
ssb.setSpan(VerticalImageSpan(drawable), start, start + atText.length, Spannable.SPAN_INCLUSIVE_EXCLUSIVE)
this.setText(ssb, TextView.BufferType.SPANNABLE)
}
VerticalImageSpan
来自精彩答案的课程
https://stackoverflow.com/a/38788432/5381331
使用
val textView = findViewById<TextView>(R.id.textview)
textView.setText("Send an [email-icon] to example@email.com.")
textView.addImage("[email-icon]", R.drawable.ic_email,
resources.getDimensionPixelOffset(R.dimen.dp_30),
resources.getDimensionPixelOffset(R.dimen.dp_30))
结果
注意
为什么是 VerticalImageSpan
课?
ImageSpan.ALIGN_CENTER
属性需要 API 29。
另外,在测试之后,我看到 ImageSpan.ALIGN_CENTER
仅在图像小于文本时有效,如果图像大于文本则只有图像在中心,文本不在中心,它在图像底部对齐>
答案 4 :(得分:0)
部分基于此earlier answer above by @A Boschman。在该解决方案中,我发现图像的输入大小极大地影响了makeImageSpan()
正确对齐图像的能力。另外,我发现该解决方案通过创建不必要的行间距来影响文本间距。
我发现BaseImageSpan(来自Facebook的Fresco图书馆)做得特别好:
/**
* Create an ImageSpan for the given icon drawable. This also sets the image size. Works best
* with a square icon because of the sizing
*
* @param context The Android Context.
* @param drawableResId A drawable resource Id.
* @param size The desired size (i.e. width and height) of the image icon in pixels.
* Use the lineHeight of the TextView to make the image inline with the
* surrounding text.
* @return An ImageSpan, aligned with the bottom of the text.
*/
private static BetterImageSpan makeImageSpan(Context context, int drawableResId, int size) {
final Drawable drawable = context.getResources().getDrawable(drawableResId);
drawable.mutate();
drawable.setBounds(0, 0, size, size);
return new BetterImageSpan(drawable, BetterImageSpan.ALIGN_CENTER);
}
然后像往常一样将你的betterImageSpan实例提供给spannable.setSpan()
答案 5 :(得分:0)
这可能对您有帮助
SpannableStringBuilder ssBuilder;
ssBuilder = new SpannableStringBuilder(" ");
// working code ImageSpan image = new ImageSpan(textView.getContext(), R.drawable.image);
Drawable image = ContextCompat.getDrawable(textView.getContext(), R.drawable.image);
float scale = textView.getContext().getResources().getDisplayMetrics().density;
int width = (int) (12 * scale + 0.5f);
int height = (int) (18 * scale + 0.5f);
image.setBounds(0, 0, width, height);
ImageSpan imageSpan = new ImageSpan(image, ImageSpan.ALIGN_BASELINE);
ssBuilder.setSpan(
imageSpan, // Span to add
0, // Start of the span (inclusive)
1, // End of the span (exclusive)
Spanned.SPAN_INCLUSIVE_EXCLUSIVE);// Do not extend the span when text add later
ssBuilder.append(" " + text);
ssBuilder = new SpannableStringBuilder(text);
textView.setText(ssBuilder);