本文介绍了部分左对齐和部分右对齐在一个TextView文本。为什么没有这方面的工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用中,我有一个应该是左对齐一些文本的TextView的,而且应该是一些文本的问题右对齐。

I'm having an issue with an textview in which i have some text that is supposed to be left aligned and some text that is supposed to be right aligned.

Here's我的尝试。

Here´s my attempt.

String LeftText = "LEFT";
String RightText = "RIGHT";


String resultText = LeftText + "  " + RightText;
SpannableString styledResultText = new SpannableString(resultText);
styledResultText.setSpan(new AlignmentSpan.Standard(Alignment.ALIGN_OPPOSITE), LeftText.length() + 1, LeftText.length() + 2 + RightText.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
textView.setText(styledResultText);

和here's为的TextView的XML。

And here´s the xml for the textview.

<TextView
    android:id="@+id/titleBar"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="Large Text"
    android:textAppearance="?android:attr/textAppearanceLarge" 
/>

然而,这是行不通的。所有文本是越来越左对齐。缺少什么我在这里?

However this is not working. All the text is getting left aligned. What am I missing here?

推荐答案

您可以使用TabStopSpan轻松解决问题pretty如果rightText是不需要右对齐对抗的边缘。

You can use TabStopSpan to solve the problem pretty easily if rightText is doesn't need to be right justified against the edge.

final TextView tv = ...
int column = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
                (float) 200.0, getResources().getDisplayMetrics());

SpannableStringBuilder textspan = new SpannableStringBuilder("Charges\nPrice\t$25.00\nShipping\t$155.08\nTax\t$5.11\n");
textspan.setSpan(new TabStopSpan.Standard(column), 0, textspan.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
tv.setText(textspan);

如果你想靠近它移动到文本的右边缘查看您可以设置使用全局布局听众布局后制表位。然后你可以使用TextView的宽度设置在接近边缘的制表位。

if you want it moved near to the right edge of your text view you can set the Tab stop after layout using a global layout listener. Then you can use the width of the textview to set a tabstop near the edge.

如果你希望它是右对齐的,你只需要做一个类扩展ReplacementSpan将对齐文本为你..

If you want it right aligned you just need to make a class extending ReplacementSpan that will justify the text for you..

这篇关于部分左对齐和部分右对齐在一个TextView文本。为什么没有这方面的工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-28 13:59