简体   繁体   English

Android编程:TextView不显示完整字符串

[英]Android Programming: TextView isn't displaying full string

I have a TextView that I am trying to make display a 2d character-based grid. 我有一个TextView,我试图使其显示基于2d字符的网格。 In my Java code I have created a 2d array, I take that array filled with strings and append each entry one next to another, and when the end of the array row is reached, i also append a newline character (.n). 在我的Java代码中,我创建了一个2d数组,我将该数组填充为字符串,并将每个条目一个接一个地追加,当到达数组行的末尾时,我还要追加一个换行符(.n)。 In this way I then take the big long singular string full of all the entries in the array (in order with the \\n) and do setText(string). 这样,我就将装满数组中所有条目的大而长的奇异字符串(按\\ n顺序)并进行setText(string)。 That way, the TextView is supposed to display a 2d grid on the display. 这样,TextView应该在显示器上显示2d网格。 It sort-of does this: the top row and the bottom row are both fine, but every row in between there are missing spaces between the end character in the row, and somewhere (approximately 13 characters) from the left side. 它的排序是这样的:顶行和底行都很好,但是中间的每一行在行的末尾字符和左侧某处(大约13个字符)之间都缺少空格。

Here is the code snippets from making the array, filling it with characters, and making that array into a long string: 以下是制作数组,用字符填充并将该数组变成长字符串的代码片段:

    for(int row = 0; row < 22; row++){
        for(int column = 0; column < 22; column++){
            if(row == 0 || row == 21){
                playField[row][column] = "#";
            }else if(row > 0 && row < 21 ){
                if(column == 0 || column == 21){
                    playField[row][column] = "#";
                }else{
                    playField[row][column] = " ";
                }                   
            }
        }

Now, the turning the array into a string: 现在,将数组变成字符串:

    temp = new String();
    for(int i = 0; i < 22; i++){
        for(int j = 0; j < 22; j++){
            temp+=playField[i][j];
        }
        temp+="\n";
    }return temp;

This string is what I use text.setText(temp) on, and it works, but it goes like this: # in column 1, about 10 blank spaces (as intended) then another # in column 13 or so, and then next row. 这个字符串是我在上面使用text.setText(temp)的字符串,它可以正常工作,但是它是这样的:第1列中的#,大约10个空格(按预期),然后在第13列中的另一个#,然后是下一行。 On the top row, there are all 22 # signs, and on the bottom row there are all 22 # signs. 在最上面的行中,所有的都是22号标志,而在最下面的行中,所有的都是22号标志。

I have my textView set as such in the xml: 我在xml中设置了我的textView:

    <TextView
    android:id="@+id/grid"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"/>

I think the problem is that you didn't declared your TextView to use the monospace typeface, so all the spaces are there, but they are not as wide as the # symbol. 我认为问题在于您没有声明TextView使用等宽字体,因此所有空格都在其中,但是它们不如#符号宽。

You can specify the typeface of the TextView like this: 您可以像这样指定TextView的字体:

android:typeface="monospace"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM