在 Eclipse 中向 TextView 添加空格以使每列固定长度

发布于 2024-12-15 09:15:16 字数 657 浏览 4 评论 0原文

我使用的文本文件存储 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

唯憾梦倾城 2024-12-22 09:15:16

这里的关键是不要仅对三列使用单个 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文