需要在一个TextView文字居中的一个部分 [英] Need to center-align a single portion of text in a TextView

查看:1077
本文介绍了需要在一个TextView文字居中的一个部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前使用SpannableString在单个TextView中设置文本格式的一大块。大小和颜色的格式就好了,但我挣扎着调整。

I'm currently using SpannableString to format a large block of text in a single TextView. The size and color are formatted just fine, but I'm struggling with alignment.

我发现这个帖子给了指导格式化对齐,以及<一个HREF =htt​​p://developer.android.com/reference/android/text/Layout.Alignment.html相对=nofollow>有关如何使用AlignmentSpan,但它没有这个Android开发者页面似乎为我工作。 Eclipse中显示没有错误,应用程序加载/运行得很好,但调整片似乎被完全忽略。这是我的code:

I found this post that gave guidance on formatting for alignment, as well as this Android Developer page on how to use AlignmentSpan, but it doesn't seem to work for me. Eclipse shows no errors, the app loads/runs just fine, but the alignment piece seems to be completely ignored. Here's my code:

Spannable t = new SpannableString("Test Blue.");
t.setSpan(new ForegroundColorSpan(getResources().getColor(R.color.color_blue)), 0, t.length()-1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
t.setSpan(new AbsoluteSizeSpan((int) getResources().getDimension(R.dimen.big_text)), 0, t.length()-1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
t.setSpan(new AlignmentSpan.Standard(Layout.Alignment.ALIGN_CENTER), 0, t.length()-1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
text3.setText(t);

任何帮助将是非常美联社preciated。

Any help would be much appreciated.

我也试着注释掉的颜色和大小setSpans既看是否有冲突,但没有运气。

I've also tried commenting out both the color and size setSpans to see if there's a conflict, but no luck.

:我标志着下面的答案正确,但正确的答案实际上是埋在意见。我无法更新我的XML文件宽度为TextView中以match_parent(而不是fill_content)。由于塞尔!

: I marked the below answer as correct, but the correct answer is actually buried in the comments. I failed to update my XML file width for the TextView to match_parent (instead of fill_content). Thanks Serge!

推荐答案

也许你可以尝试这样做:

Maybe you could try to do this:

String t = "<center><font color=\"blue\">Test Blue.</font></center>";
text3.setText(Html.fromHtml(t)); 

这篇关于需要在一个TextView文字居中的一个部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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