对准ImageSpan到TextView的顶部 [英] Aligning ImageSpan to the top of the TextView
问题描述
目前,我想补充的图像文本之间的,它对准的TextView顶部。
事情是这样的:
唯一的垂直对齐我能看到的是基准(这似乎说得没错下来的文本的中心),并调整底部。
如果我用ALIGN_BASELINE是会发生什么:
有没有一种方法,将其调整到顶部呢?
我目前的code:
txtView.setText(this.addImageAsterisk(
字符串有星号在年底*),BufferType.SPANNABLE);
然后
私人CharSequence的addImageAsterisk(字符串字符串){
绘制对象D = context.getResources()getDrawable(R.drawable.img_asterisk)。
ImageSpan imageSpan =新ImageSpan(D,ImageSpan.ALIGN_BASELINE);
最后SpannableString spannableString =新SpannableString(串);
spannableString.setSpan(imageSpan,string.length减() - 1,string.length减(),0);
返回spannableString;
}
删除ImageSpan.ALIGN_BASELINE其设置对齐到这也不是我期望的结果的底部。
---谢谢用户拉利特Poptani,我试着将你的答案---- 应用此之后,会发生什么情况是,整个TextView中似乎有额外的保证金之上。
应用范围之前:
这是文本*
应用SuperscriptSpanAdjuster后
(一些额外的空间)
这是文本*
我的code:
字符串字符串=这是文本*;
绘制对象D = this.context.getResources()getDrawable(R.drawable.img_asterisk)。
d.setBounds(0,0,d.getIntrinsicWidth(),d.getIntrinsicHeight());
ImageSpan imageSpan =新ImageSpan(D,ImageSpan.ALIGN_BASELINE);
SuperscriptSpanAdjuster S =新SuperscriptSpanAdjuster(1.5);
最后SpannableString spannableString =新SpannableString(串);
spannableString.setSpan(S,string.length减() - 1,string.length减(),0);
spannableString.setSpan(imageSpan,string.length减(),string.length减()+ 1,0);
textView.setText(spannableString);
你可以做的是使用自定义的 MetricAffectingSpan
为保持其比例一样,
公共类SuperscriptSpanAdjuster扩展MetricAffectingSpan {
双比= 0.5;
公共SuperscriptSpanAdjuster(双比){
this.ratio =比;
}
@覆盖
公共无效updateDrawState(TextPaint漆){
paint.baselineShift + =(int)的(paint.ascent()*比率);
}
@覆盖
公共无效updateMeasureState(TextPaint漆){
paint.baselineShift + =(int)的(paint.ascent()*比率);
}
}
和的,你可以使用 SpannableString
应用星号的字符串一样,
SpannableString mString =新SpannableString(这就是我想要的东西*);
mString.setSpan(新SuperscriptSpanAdjuster(0.5),mString.length() - 1,
mString.length(),SpannableString.SPAN_EXCLUSIVE_EXCLUSIVE);
mTextView.append(\ N);
mTextView.append(mString);
这是添加星号,以文字,你需要的。而你的输出应该是,
Currently, I wish to add an image in between texts and align it to the top of the TextView.
Something like this:
The only vertical alignments I can find are baseline (which seems to put it right down the center of the text) and align bottom.
What happens if I use ALIGN_BASELINE is:
Is there a way to align it to the top instead?
My current code:
txtView.setText(this.addImageAsterisk(
"The string to have asterisk at the end*"), BufferType.SPANNABLE);
then
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;
}
removing ImageSpan.ALIGN_BASELINE sets it to align to the bottom which is also not my expected result.
--- Thank you user Lalit Poptani, I tried applying your answer---- after applying this, what happens is that the whole textview seems to have extra margin top.
before applying span:
This is the text*
after applying the SuperscriptSpanAdjuster
(some extra space)
This is the text*
My code:
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);
What you can do is use a custom MetricAffectingSpan
for maintaining its ratio like,
public class SuperscriptSpanAdjuster extends MetricAffectingSpan {
double ratio = 0.5;
public SuperscriptSpanAdjuster(double ratio) {
this.ratio = ratio;
}
@Override
public void updateDrawState(TextPaint paint) {
paint.baselineShift += (int) (paint.ascent() * ratio);
}
@Override
public void updateMeasureState(TextPaint paint) {
paint.baselineShift += (int) (paint.ascent() * ratio);
}
}
And the you can use SpannableString
to apply asterisk to your String like,
SpannableString mString = new SpannableString("This is what I wanted*");
mString.setSpan(new SuperscriptSpanAdjuster(0.5), mString.length() - 1,
mString.length(), SpannableString.SPAN_EXCLUSIVE_EXCLUSIVE);
mTextView.append("\n");
mTextView.append(mString);
This is append asterisk to your text as you required. And your output will be as,
这篇关于对准ImageSpan到TextView的顶部的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!