How to align the output text in order

北城以北 提交于 2021-02-11 18:02:04

问题


I am facing a problem aligning the text in proper format

EX: say I am to get printout in the following way

Name                     Phone No.
Adithya                  99999999999 
Ravi                     99999999999 
Teja                     99999999999
:
:

but as the length of the String Adithya is not equal to length of Ravi we cannot give specific spacing such as

String s = Name+ " " + Phone + "\n";

here if I give so I'll get in the following way

Name                Ph
Adithya              9999999
Ravi              9999999
Tej              9999999
:
:

it will display in this way. How can I solve this?


回答1:


This problem is exactly what a ListView is designed for.

Use a ListView and you can place the Name on the left and the Phone on the right, or any other orientation. You can easily change colors, text size, font, etc. Find an example for ListView. The easy to use simple-adapter is perfect for this implementation. You should be good to go.


I modify this answer here because you have stated in the comment below that you wish to format the phone number list for email and print, rather than for on-screen display.

This is now a difficult problem for several reasons:

  • Email doesn't obey tabs.
  • Tabs are non-optimal because longer and shorter names will need a different number of tab characters.

The only solution I can think of for email and general print output is to calculate the number of space characters necessary between the name and phone number columns* and force printing with a mono-space font such as "Courier", etc.

(*) How to calculate the number of spaces required between columns: Choose a row position for the phone number column, i.e. 40 characters. The number of space characters required between name and phone for any specific row is (40 - name.length()).

Your code will look something like this:

String text = "Phone List:";
for(i = 0; i < n; i++) {
    String name = names[i];
    String phone = phones[i];
    int nsp = 40 - name.length();
    text += "\n" + name + StringUtils.repeat(" ", nsp) + phone;
}



回答2:


You could use tab (\t):

String s = String.format("%s\t\t%s\n", Name, Phone);

This will enter two tabs after the name and then the phonenumber. The phonenumbers should be aligned nicely.




回答3:


I would rather suggest you to use Table Layout (it will guarantee you alignment) instead of making your code complex.

The following will be the layout for your list view ROW Your coding part will be same just switch to this new layout

   <?xml version="1.0" encoding="utf-8"?>
    <TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent" 
        android:stretchColumns="1">


        <TableRow
            android:id="@+id/tblRow1"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"        
            android:padding="5dip">


      <TextView 
          android:id="@+id/tv_Name" 
          android:layout_width="0dip"
          android:layout_weight="3"   <!--it defines how much space this view should get in row-->
          android:text="Ravi"
          android:layout_height="wrap_content" 
          android:paddingLeft="20dip"
          android:textSize="14sp"
          android:textColor="@android:color/black"/>

       <TextView 
          android:id="@+id/tv_PhoneNumber" 
          android:layout_width="0dip"
          android:layout_weight="3"
          android:text="9999999999"
          android:layout_height="wrap_content" 
          android:paddingLeft="20dip"
          android:textSize="14sp"
          android:textColor="@android:color/black"/>   

      </TableRow>

    </TableLayout>

enter image description here

Then bind this layout to your adapter like you do with other Layouts..



来源:https://stackoverflow.com/questions/15576406/how-to-align-the-output-text-in-order

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!