在 Eclipse 中向 TextView 添加空格以使每列固定长度
我使用的文本文件存储 3 列数据,每列数据的长度各不相同。
这是我迄今为止尝试过的以下代码。我希望每一列都左对齐。由于某种原因,如果我使用星号,它会起作用,而如果我尝试手动插入空格,则它不会起作用。
(行从我的文本文件中读取 3 个单词)
尝试#2:
while((line = buf.readLine())!= null){
StringTokenizer st = new StringTokenizer(line);
int length = 12;
a = st.nextToken();
while (a.length() <= length)
{
a = a + "*";
}
b = st.nextToken();
while (b.length() <= length)
{
b = b + "*";
}
c = st.nextToken();
text.append(a + b + c + '\n');
}
这是我最初的尝试,但这也不起作用: text.append(String.format("%-15s\t %-10s\t %-5s\t \n", a, b, c));
任何想法将不胜感激。
I am using a text file that stores 3 columns of data, each having varying length.
This is the following code I have tried so far. I want each column to be left justified. For some reason it works if I use an asterisk and not if I try to manually insert a whitespace.
(line is reading in 3 words from my text file)
Attempt #2:
while((line = buf.readLine())!= null){
StringTokenizer st = new StringTokenizer(line);
int length = 12;
a = st.nextToken();
while (a.length() <= length)
{
a = a + "*";
}
b = st.nextToken();
while (b.length() <= length)
{
b = b + "*";
}
c = st.nextToken();
text.append(a + b + c + '\n');
}
This was my original attempt but this did not work either:
text.append(String.format("%-15s\t %-10s\t %-5s\t \n", a, b, c));
Any ideas would be greatly appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这里的关键是不要仅对三列使用单个 TextView。您希望为每一列使用不同的 TextView,并单独控制每个 TextView 的格式。您使用什么类型的布局?如果您切换到表格布局,应该很容易左边证明你想要什么。如果您的文件长度可变,请使用 ListView每行有三个不同的文本视图,然后左对齐每个文本视图。
The key here is not to use just a single TextView for your three columns. You want to use a different TextView for each column and the control the formatting of each TextView individually. What type of layout are you using? If you switch to a table layout, it should be easy to left justify what ever you want. If your file will have a variable length, use a ListView which has three different text views in each row and then left justify each textview.