0

在我的应用程序的一项活动中,我正在显示用户名,并在名称前显示他们的点。

名称可能有不同数量的字符,但我希望字符串在同一个地方结束。

能够提供正确数量的空格,并且字符串的字符长度相等,字符占用的空间不均匀破坏了字符串的对称性

有什么解决方法吗?

这是代码

private String separateNamePoints{
    String text="";

            //text is separated by ,
    String[]splittedRawText=rawText.split(",");

    String name=(splittedRawText)[0];
    String points=(splittedRawText)[1];

    int pointsLength=points.length();
    int reqSpaceLength=40-name.length();

    String space="";

    for(int i=0;i<reqSpaceLength;i++){
        space+=" ";
    }

    if(pointsLength==1)
        space+="   ";
    else if(pointsLength==2)
        space+="  ";
    else if(pointsLength==3)
        space+=" ";

    text=name+space+points;

    return text;
}

是图像

在此处输入图像描述

4

4 回答 4

0

Android 不提供完整的文本理由

在这种情况下,需要创建一个包含单独编辑文本的自定义控件来显示数据。

于 2013-10-03T06:12:42.757 回答
0

尝试将此 xml 用于列表项并为此编写适配器:

<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >

    <TextView
        android:id="@+id/name" 
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="4"/>

    <TextView
        android:id="@+id/points" 
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"/>

</LinearLayout>
于 2013-06-18T08:14:33.123 回答
0

有办法处理这个..

  • 你的方法

在这里,您可以尝试String.format()根据要求使用格式化.. 在这里阅读此供参考

  • 您可以使用 2 textViews 并使用对齐来解决您的问题..
于 2013-06-18T08:12:22.917 回答
-1

您的代码将正常工作。我认为您的输入字符串在数字后有一些空格。请试试这个

String[]splittedRawText=rawText.trim().split(",");
于 2013-06-18T08:30:20.483 回答