我过去几天一直试图弄清楚这一点,并没有成功......
我现在正在学习机器人,目前正在创建一个带有历史记录的计算器作为我的学习项目.我有一个TextView负责显示所有历史...我使用的数字字体看起来像一个计算器字体,但这只适用于数字,小数和逗号.我希望所有操作符都以不同的字体突出显示(Arial Narrow).我已经能够使用spannable字符串精美地工作,我指定字体颜色以及使用CustomTypeFaceSpan类来应用我的自定义字体的字体.
问题...当我混合使用字体时,行高似乎存在问题,所以我发现这篇文章演示了如何使用另一个自定义类来为每个添加的spannable文本行应用行高:
public class CustomLineHeightSpan implements LineHeightSpan{
private final int height;
public CustomLineHeightSpan(int height){
this.height = height;
}
@Override
public void chooseHeight(CharSequence text, int start, int end, int spanstartv, int v, FontMetricsInt fm) {
fm.bottom += height;
fm.descent += height;
}
}
Run Code Online (Sandbox Code Playgroud)
这似乎不起作用,我无法弄清楚为什么.如果我不应用不同的字体,那么它会按预期显示,第一行上方没有空格,行间距约为5px.当我应用替代字体时,在第一行文本上方有一个大约10到15px的空间,行间距大约相同10到15px.
字体大小没有区别,只有字体.我错过了什么 我实现了CustomLineHeightSpan,它实现了LineHeightSpan并覆盖了chooseHeight方法.我称之为:
WordtoSpan.setSpan(new CustomLineHeightSpan(10),operatorPositions.get(ii),operatorPositions.get(ii)+ 1,Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
我对CustomLineHeightSpan的调用似乎并不重要.没有什么变化...
任何人都知道我错过了什么......我确信这是一个"我不敢相信我错过了"的答案,但目前似乎无法弄明白.
谢谢你们的帮助:-)
目前,我希望在文本之间添加一个图像,并将其对齐到TextView的顶部.
像这样的东西:

我能找到的唯一垂直对齐是基线(似乎将它放在文本的中心)并对齐底部.
如果我使用ALIGN_BASELINE会发生什么:

有没有办法将它与顶部对齐?
我目前的代码:
txtView.setText(this.addImageAsterisk(
"The string to have asterisk at the end*"), BufferType.SPANNABLE);
Run Code Online (Sandbox Code Playgroud)
然后
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;
}
Run Code Online (Sandbox Code Playgroud)
删除ImageSpan.ALIGN_BASELINE将其设置为与底部对齐,这也不是我预期的结果.
---感谢用户Lalit Poptani,我尝试应用你的答案----应用之后,会发生的事情是整个textview似乎有额外的余量.
在应用范围之前:
This is the text*
Run Code Online (Sandbox Code Playgroud)
在应用SuperscriptSpanAdjuster之后
(some extra space)
This is the text*
Run Code Online (Sandbox Code Playgroud)
我的代码:
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 = …Run Code Online (Sandbox Code Playgroud) I have TextView with spans of type ClickableStringSpan defined as below:
public class ClickableStringSpan extends ClickableSpan {
private View.OnClickListener mListener;
int color;
public ClickableStringSpan(View.OnClickListener listener,int color) {
mListener = listener;
this.color = color;
}
@Override
public void onClick(View v) {
mListener.onClick(v);
}
@Override public void updateDrawState(TextPaint ds) {
super.updateDrawState(ds);
ds.setUnderlineText(false);
ds.setColor(color);
}
}
Run Code Online (Sandbox Code Playgroud)
I set clickable spans on my text like this:
spanStr.setSpan(new ClickableString(new linkedTextClickListener(), linkColor),
startIndex, endIndex,
SpannableString.SPAN_INCLUSIVE_EXCLUSIVE);
Run Code Online (Sandbox Code Playgroud)
Now I want to apply these string to EditTexts instead of …
我想将onTouchListeners分配给TextView中的每个单词.(不是链接到互联网上的东西,而只是为了继续应用程序内的游戏逻辑).此时我的游戏的一般动作是看到TextView,触摸一个单词,如果它是你赢得的目标单词,否则根据你触摸和重复的单词加载另一个TextView.我现在实现这一目标的方法是使用ClickableSpans和每个单词的onClicks.
但我宁愿拥有onTouchListeners,所以我可以在touch_down上更改单词背景的颜色,并在touch_up上执行游戏逻辑,使其看起来更具响应性.我怎么能做到这一点?
final TextView defTV = (TextView) findViewById(R.id.defTV);
text = new SpannableString(rv); // rv is the future clickable TextView text
ClickableSpan clickableSpan = null;
String regex = "\\w+";
Pattern p = Pattern.compile(regex);
Matcher matcher = p.matcher(text);
while (matcher.find()) {
final int begin = matcher.start();
final int end = matcher.end();
clickableSpan = new ClickableSpan() {
public void onClick(View arg0) {
String lword = (String) text.subSequence(begin, end).toString();
if (lword.equalsIgnoreCase(targetword)) {
// WIN
} else {
// Build new TextView based on lword, …Run Code Online (Sandbox Code Playgroud) 我已将SpannableString设置为EditText,现在我想从EditText获取此文本并获取其标记信息.我试过这样的
SpannableStringBuilder spanStr = (SpannableStringBuilder) et.getText();
int boldIndex = spanStr.getSpanStart(new StyleSpan(Typeface.BOLD));
int italicIndex = spanStr.getSpanStart(new StyleSpan(Typeface.ITALIC));
Run Code Online (Sandbox Code Playgroud)
但它为粗体和斜体提供了索引-1,尽管它显示的是带斜体和粗体的文本.
请帮忙.
我在iOS应用程序上应该能够突出显示文本,并使其可以点击.
我在iOS中读到了关于NSAttributedString但它仍然比android中的Spannable更复杂.
有没有其他Objective c方法可以做到这一点,如果没有; 我该怎么做NSAttributedString才能逐字逐句地突出显示一个段落,以及如何使我的文字可以点击.
更新:
我究竟想要的是每个单词都应该是可点击的,并且可以 在一个段落中突出显示为单个单词.
objective-c nsattributedstring core-text ios spannablestring
我做了一个
安卓 应用程序有一个按钮,其中总共有4个文本,我想对齐第一个2.一个位于底部文本的最左侧,另一个位于底部文本的右侧.
所以从这个:

setText(item.title + " " + item.roomId + "\n" + item.teacher + " "
+ item.classes);
对此:

setText(declare here a spannable);
我想我应该一起工作Spannable,我已经尝试了一些事情Alignment.ALIGN_NORMAL和Alignment.ALIGN_OPPOSITE,但我认为是应该首先计算底部文本的长度,然后做了比对.(我在这里找到了一个很好的例子,但它在我的设置中没有用).
我希望有人能指出我的方向.
编辑:
我不能(我认为)使用RelativeLayout或是LinearLayout因为我在另一个类(ScheduleItemView.java)中扩展按钮的原因:
/**
* Custom view that represents a {@link ScheduleItem} instance, including its
* title and time span that it occupies. Usually organized automatically by
* {@link ScheduleItemsLayout} to match up against a {@link TimeRulerView}
* instance. …Run Code Online (Sandbox Code Playgroud) 我有两个String资源:
<string name="give_us_feedback">Give us feedback at %1$s if you want to make the app even better!</string>
<string name="email">info@mycompany.com</string>
Run Code Online (Sandbox Code Playgroud)
我想将电子邮件部分设置为蓝色和下划线,以指示用户可以单击它(整个TextView,而不仅仅是电子邮件文本).我知道使用SpannableString来为文本着色,但是当我通过getString(int resId,Object ... formatArgs)组合两个字符串时它似乎不起作用,可能是因为getString()将执行强制转换或.toString ()正在发送的对象上.这是不起作用的:
TextView emailTV = new TextView(this);
SpannableString email = new SpannableString(getString(R.string.email));
email.setSpan(new UnderlineSpan(), 0, email.length() - 1, 0);
email.setSpan(new ForegroundColorSpan(Color.BLUE), 0, email.length() - 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
String feedback = getString(R.string.give_us_feedback, email);
emailTV.setText(feedback);
Run Code Online (Sandbox Code Playgroud)
有任何想法吗?
我有一个标签云,其中标签有背景颜色.不幸的是,我无法获得行间距.
我们假设这是文本云:
tag1 tag2 tag3
tagtext4 tagtext5
Run Code Online (Sandbox Code Playgroud)
这是文本视图的样式:
<style name="DataEntryValue" parent="@android:style/TextAppearance.Medium">
<item name="android:textColor">@color/gray_value</item>
<item name="android:fontFamily">sans-serif-condensed</item>
</style>
Run Code Online (Sandbox Code Playgroud)
BackgroundColorSpan用于为标签设置蓝色背景颜色.然而,线之间没有空间,即两条线的背景颜色没有分开.
当我将文本视图的行间距设置为12sp时,它会添加行间距,但使用标记的背景颜色而不是textview背景颜色来建立行间距.
知道如何使用文本视图背景颜色获得行间距?
更新
使用此解决方案作为指导我想出了这种方法:绘制文本的背景,在顶部和底部用填充绘制文本的背景,绘制文本.因为我之前没有使用过画布,所以可能有更好的方法.无论如何 - 这是代码.
public void draw(Canvas canvas, CharSequence text, int start, int end, float x,
int top, int y, int bottom, Paint paint)
{
float padding;
float right
RectF rect;
right = x + measureText(paint, text, start, end);
padding = 4f;
rect = new RectF( x, top, right, bottom );
paint.setColor( mContext.getResources().getColor(R.color.color1) );
rect = new RectF( x, …Run Code Online (Sandbox Code Playgroud) android string-formatting spacing android-canvas spannablestring
我有一个像这样的3字符串:
"@Username: Deliverd your order",
"YOU got trophy: KING OF COINS",
"There is a package waiting for you to pick up from #surat to #mumbai",
Run Code Online (Sandbox Code Playgroud)
我想做的是通过点击事件获得不同颜色的用户名和城市名称.
能够实现的是通过分割为":"字符来获取用户名.但我不知道如何获得城市名称和点击两者的事件.
在城市名称中,只有最后一个城市颜色在变化,如何更改城市名称颜色并获取其点击事件.
这是我试过的:
if (notifications.getTitle().contains(":"))
{
String[] username = notifications.getTitle().split(":");
String uname = getColoredSpanned(username[0] + ":", "#ff7505");
String txt = getColoredSpanned(username[1], "#000000");
holder.txtTitle.append(Html.fromHtml(uname +" " + txt));
holder.txtTitle.setMovementMethod(LinkMovementMethod.getInstance());
}
else if (notifications.getTitle().contains("#"))
{
Matcher matcher =
Pattern.compile("#\\s(\\w+)").matcher(notifications.getTitle());
i=0;
while (matcher.find())
{
place.add(i, matcher.group(1));
i++;
}
String place1 = getColoredSpanned("#" + place.get(0), "#237BCD");
String place2 …Run Code Online (Sandbox Code Playgroud)