复选框内的Android文本对齐问题

时间:2013-06-18 08:02:51

标签: java android string android-edittext android-checkbox

在我的应用程序的一个活动中,我正在显示名称前面的用户名。

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

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

有没有解决这个问题?

以下是代码

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;
}

这是图片

enter image description here

4 个答案:

答案 0 :(得分:0)

有办法可以解决这个问题。

  • 您的方法

在这里,您可以尝试使用String.format()根据要求进行格式化..这里read this for ref

  • 您可以使用2个textView并使用对齐来解决您的问题..

答案 1 :(得分: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>

答案 2 :(得分:0)

Android不提供完整的文字说明

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

答案 3 :(得分:-1)

您的代码可以正常运行。我认为您的输入字符串在数字后面有一些空格。 请尝试使用此

String[]splittedRawText=rawText.trim().split(",");
相关问题