改变换行行为 [英] Altering line wrap behavior
问题描述
我可以在 TextViews 中使用 Spannable 来创建具有不同外观、下划线、删除线等的跨度.我该如何做同样的事情来改变换行行为?特别是,我不希望电子邮件地址在中间换行,我希望它表现得像一个词.
I can use Spannable in TextViews to create spans with different looks, underlines, strikethroughs and such. How can I do the same to alter line wrapping behavior? In particular, I don't want an email address to wrap in the middle, I want it to act like one word.
我尝试了 WrapTogetherSpan,但我无法得到它工作.貌似只有DynamicLayout使用,不能强制TextView使用DynamicLayout.
I tried WrapTogetherSpan, but I couldn't get it to work. It looks like it is only used by DynamicLayout, and I could not force the TextView to use DynamicLayout.
<TextView
android:id="@+id/merchant_email_field"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="@dimen/account_setting_email"
android:gravity="center"
android:bufferType="spannable"
android:maxLines="2"
android:ellipsize="end"
/>
我如何设置可跨度:
WrapTogetherSpan TOGETHER_SPAN = new WrapTogetherSpan() {};
String collectedString = getString(R.string.email_sentence, userEmail);
int emailOffset = collectedString.indexOf(userEmail);
Spannable emailSpannable = Spannable.Factory.getInstance()
.newSpannable(collectedString);
emailSpannable.setSpan(TOGETHER_SPAN, emailOffset,
emailOffset + userEmail.length(),
Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
textView.setText(emailSpannable)
推荐答案
不知道您是否找到了答案,但您可以使用 unicode 来帮助您.
Don't know if you have found an answer to it but you can use unicode to help you out.
有一个不间断的空格字符,所以你需要用这个字符(\u00A0)替换所有你想要牢不可破的空格
there is a non-break space character, so you will need to replace all the spaces you want to be unbreakable with this character (\u00A0)
举个例子
String text = "Hello World";
text.replace(' ', '\u00A0');
textView.setText(text);
顺便说一下,我一直在寻找 span 解决方案,但找不到,WrapTogetherSpan 只是一个接口,所以它不起作用...
by the way I've looked for a span solution and couldn't find one, WrapTogetherSpan is just an interface so it wouldn't work...
但是使用这种方法,如果您愿意,我相信您可以制作一个自定义的牢不可破的跨度.
but with this method I'm sure you could make a custom unbreakable span if you want.
这篇关于改变换行行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!