给定Horizo​​ntalFieldManager中的两个标签字段,如何在不包装的情况下显示第二个标签的全文?

时间:2013-12-18 20:40:53

标签: java blackberry labelfield horizontalfieldmanager

我有一个里面有两个标签的Horizo​​ntalFieldManager。 左侧标签显示说明,右侧标签显示 金额。

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

这就是Horizo​​ntalFieldManager的外观: enter image description here

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

我该怎么做?

提前致谢!

2 个答案:

答案 0 :(得分:3)

如果您使用LabelField标记创建LabelField.ELLIPSIS,则会使用.字符截断字段。我建议您使用自定义Manager子类(而不是HorizontalFieldManager)来确定两个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

答案 1 :(得分:1)

见例子:

class Test {
    public String StringShorter(String field, int maxsize) {
    //create a function that process the String you want to put in your field
    StringBuilder strb=new StringBuilder();
    // lets say you want your field to not more than 10 characters

        if(field.length()>=maxsize) {
            strb.append(field.substring(0,maxsize));
            strb.append("...");
        }
        return strb.toString();
    }

    public static void main(String[] args) {
        Test sl=new Test();
        System.out.println(sl.StringShorter("sdadasfdfsdfsdfsdfdsffdfs", 10)); 
        // define the maximum characters here it is defined to be maximum 10 characters
        }
    }
输出将是:

sdadasfdfs...