Android创建不包装的Spannable [英] Android create Spannable which does not wrap

查看:87
本文介绍了Android创建不包装的Spannable的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Android上布置文本时遇到以下问题.我基本上是在尝试使两行文本的间距最小,并且每行的样式都应不同.我有一个很好的工作解决方案,将两个单行TextViews放置在另一个下面,但是在某些设备上我仍然得到了一些裁剪的文本.

I have a following issue with laying out text on Android. I'm basically trying to have two lines of text with minimal spacing and each should be styled differently. I've had quite good working solution with two singlelined TextViews one placed below the other, but I've been still getting a little bit cropped text on certain devices..

所以我决定只改用一个TextView,而要使用Spannables,这通常在所有情况下都是更好的解决方案.

So I decided to switch to just one TextView an use Spannables instead which should be generally a better solution in all circumstances.

这意味着我需要从TextView->中删除单行属性,以便能够在开始第二个Spannable之前对行进行换行.但是,当第一行显示的文本实际上更长时,存在一个问题比它..TextView自动包装这是不受欢迎的行为.在下面,您可以看到几个屏幕截图,您最好将它们告诉我正在努力实现的目标以及现在的位置.

That means I needed to remove the single line property from my TextView -> in order to be able to wrap the line before starting the second Spannable..But there is an issue when is the text displayed at the first line actually longer than it..TextView wraps Automaticaly which is an unwanted behavior. Below you can see several screenshots, which should you better tell what I'm trying to achieve and where I'm now.

第一张图显示了具有跨度的新布局,您也可以在其中看到换行. 第二个图像是布局的初始版本,两个文本视图在LinearLayout中垂直布置.

The first image shows new layout with spannables and you can see there the wrapped line as well. The second image is the initial version of the layout woth two textviews layed out verically in a LinearLayout.

还有一个问题,它实际上是一个appwidget,这意味着我无法直接访问该textview实例.我一直在考虑放弃文本视图,而是只使用ImageView并在画布上手动渲染所有文本.不幸的是,我有点想法和知识不足:)

There is also a problem it's actually an appwidget, that means I do not have an access to that textview instance directly. I have been thinking about ditching textviews at all and instead use just ImageView and render all manually on canvas..That seems like an overkill to me, so I'm looking for a better solution. Unfortunately I'm kind of out of ideas and knowledge:)

谢谢

推荐答案

如果要防止多字字符串换行,可以将空格替换为

If you want to prevent a multi-word string from wrapping, you can replace the spaces with non-breaking spaces ('\u00A0'). TextView treats these as word characters, but renders them as spaces.

这篇关于Android创建不包装的Spannable的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆