gpt4 book ai didi

java - 给定 Horizo​​ntalFieldManager 中的两个标签字段,我应该如何显示第二个标签的全文而不换行?

转载 作者:行者123 更新时间:2023-11-29 03:26:45 24 4
gpt4 key购买 nike

我有一个内部有两个标签的 Horizo​​ntalFieldManager。左侧标签显示描述,右侧显示金额。

对我来说,显示全文更重要第二个标签。问题是如果第一个标签太长,第二个标签将被包裹。我想避免这种情况,所以始终显示来自第二个标签的文本。我也需要避免在这种情况下,包装在第一个标签上,所以来自该标签的文本被修剪并用点填充。

这是 Horizo​​ntalFieldManager 的样子: enter image description here

这就是我需要得到的: enter image description here

我应该怎么做?

提前致谢!

最佳答案

如果您使用 LabelField.ELLIPSIS 标志创建您的 LabelField,它将截断带有 . 字符的字段。我建议您使用自定义 Manager 子类(而不是 Horizo​​ntalFieldManager)来决定您的两个 LabelFields 的正确宽度应该是多少。在给定当前字体的情况下,您可以通过询问美元金额的适当宽度来做到这一点。

试试这个例子:

public class LabelAlignScreen extends MainScreen {

private LabelField description;
private LabelField balance;
private static final int MARGIN = 8; // used for x and y

public LabelAlignScreen() {
super(MainScreen.VERTICAL_SCROLL | MainScreen.VERTICAL_SCROLLBAR);

Manager row = new RowManager(Manager.USE_ALL_WIDTH);
description = new LabelField("This is a very looooooooooong description",
LabelField.ELLIPSIS);
row.add(description);
balance = new LabelField("1,500,000,000 USD");
row.add(balance);
add(row);
}

private class RowManager extends Manager {
public RowManager(long flags) {
super(flags);
}

protected void sublayout(int width, int height) {
// first, determine how much space the balance field needs
int balanceWidth = balance.getFont().getAdvance(balance.getText());
// description field gets leftover width,
// minus a margin at left, center and right
int descriptionWidth = width - balanceWidth - 3 * MARGIN;

setPositionChild(description, MARGIN, MARGIN);
layoutChild(description, descriptionWidth, description.getPreferredHeight());

setPositionChild(balance, MARGIN + descriptionWidth + MARGIN, MARGIN);
layoutChild(balance, balanceWidth, balance.getPreferredHeight());

setExtent(width, getPreferredHeight());
}

public int getPreferredHeight() {
return Math.max(balance.getPreferredHeight(), description.getPreferredHeight()) + 2 * MARGIN;
}

public int getPreferredWidth() {
return Display.getWidth();
}
}
}

注意:您没有指定美元/余额字段是否应该是固定宽度,或者总是刚好足以适合文本。我认为它应该刚好适合文本,因为我认为在大多数情况下这有助于更好的布局。此外,我上面的代码对所有字段周围的空间使用了硬编码的 MARGIN 值。您可以根据需要进行调整。

结果

enter image description here

关于java - 给定 Horizo​​ntalFieldManager 中的两个标签字段,我应该如何显示第二个标签的全文而不换行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20667947/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com