目前,我希望在文本之间添加一个图像并将其对齐到 TextView 的顶部。
像这样的东西:
我能找到的唯一垂直对齐方式是基线(似乎将它放在文本中心的正下方)和底部对齐。
如果我使用 ALIGN_BASELINE 会发生什么:
有没有办法让它与顶部对齐?
我当前的代码:
txtView.setText(this.addImageAsterisk(
"The string to have asterisk at the end*"), BufferType.SPANNABLE);
然后
private CharSequence addImageAsterisk(String string) {
Drawable d = context.getResources().getDrawable(R.drawable.img_asterisk);
ImageSpan imageSpan = new ImageSpan(d, ImageSpan.ALIGN_BASELINE);
final SpannableString spannableString = new SpannableString(string);
spannableString.setSpan(imageSpan, string.length()-1, string.length(), 0);
return spannableString;
}
删除 ImageSpan.ALIGN_BASELINE 将其设置为与底部对齐,这也不是我的预期结果。
--- 谢谢用户 Lalit Poptani,我尝试应用您的答案---- 应用此之后,发生的情况是整个 textview 似乎有额外的边距顶部。
在应用跨度之前:
This is the text*
应用 SuperscriptSpanAdjuster 后
(some extra space)
This is the text*
我的代码:
String string = "This is the text*";
Drawable d = this.context.getResources().getDrawable(R.drawable.img_asterisk);
d.setBounds(0, 0, d.getIntrinsicWidth(), d.getIntrinsicHeight());
ImageSpan imageSpan = new ImageSpan(d, ImageSpan.ALIGN_BASELINE);
SuperscriptSpanAdjuster s = new SuperscriptSpanAdjuster(1.5);
final SpannableString spannableString = new SpannableString(string);
spannableString.setSpan(s, string.length() - 1, string.length(), 0);
spannableString.setSpan(imageSpan, string.length(), string.length() + 1, 0);
textView.setText(spannableString);