【问题标题】:Making the RichTextField Horizontally Centered in Blackberry使 RichTextField 在 Blackberry 中水平居中
【发布时间】:2012-10-20 13:25:05
【问题描述】:

我当前的主屏幕看起来像:

当前每一行显示的代码是这样的:

    RichTextField WeFix1 = new RichTextField("• Computer & Laptop", RichTextField.TEXT_JUSTIFY_HCENTER);

    VFMTitle1 = new VerticalFieldManager(Field.USE_ALL_WIDTH| Field.NON_FOCUSABLE);

    HFMTitle1 = new HorizontalFieldManager(FIELD_HCENTER);

    HFMTitle1.add(WeFix1);

  VFMTitle1.add(HFMTitle1);
add(VFMTitle21);

但需要定位在一条直线上:

【问题讨论】:

  • 表示您是否希望每行的背景为灰色?或者你需要直线最右边和最左边的屏幕位置?
  • 尝试使用标签字段而不是 RichTextField
  • 因为 RichTextField 占据了所有的宽度,并且它没有在旁边添加任何字段。这时候你必须使用 sublayout() 方法来提供一些宽度和高度;并在屏幕中间放置一些填充。这就是为什么最好使用 LabelField

标签: layout blackberry field center richtext


【解决方案1】:

有很多可能性。您可以调整包含服务列表的容器的边距。此外,您还可以自定义字段管理器等等。

选项 1:调整边距

VerticalFieldManager vfmServices = new  VerticalFieldManager(VerticalFieldManager.USE_ALL_WIDTH);

vfmServices.add(new RichTextField("• Computer & Laptop"));
vfmServices.add(new RichTextField("• Modem / Router / Switches"));
vfmServices.add(new RichTextField("• Printer / Scanner"));
vfmServices.add(new RichTextField("• Tablet"));

final int horizontalMargin = 30;
vfmServices.setMargin(0, horizontalMargin, 0, horizontalMargin);

add(vfmServices);

选项 2:使用自定义字段管理器

VerticalFieldManager vfmServiceLists = new VerticalFieldManager(VerticalFieldManager.USE_ALL_WIDTH);

vfmServiceLists.add(new RichTextField("• Computer & Laptop"));
vfmServiceLists.add(new RichTextField("• Modem / Router / Switches"));
vfmServiceLists.add(new RichTextField("• Printer / Scanner"));
vfmServiceLists.add(new RichTextField("• Tablet"));

Manager mListContainer = new Manager(Manager.USE_ALL_WIDTH) {

    final int horizontalMargin = 30;

    protected void sublayout(int width, int height) {
        // this manager designed to contain only one list container.
        if (getFieldCount() == 1) {
            Field child = getField(0);
            layoutChild(child, width - 2 * horizontalMargin, height);
            // adjust manager height.
            height = child.getHeight();
            setPositionChild(child, horizontalMargin, 0);
        }

        setExtent(width, height);       
    }           
};

mListContainer.add(vfmServiceLists);
add(mListContainer);

[稍后添加]

正如alishaik786在评论中建议的那样,如果您使用LabelField而不是RichTextField,您可以检查以下代码,该代码不使用任何边距:

VerticalFieldManager vfmServiceLists = new VerticalFieldManager();

vfmServiceLists.add(new LabelField("• Computer & Laptop"));
vfmServiceLists.add(new LabelField("• Modem / Router / Switches"));
vfmServiceLists.add(new LabelField("• Printer / Scanner"));
vfmServiceLists.add(new LabelField("• Tablet"));

Manager mListContainer = new Manager(Manager.USE_ALL_WIDTH) {
    protected void sublayout(int width, int height) {
        // this manager designed to contain only one list container.
        if (getFieldCount() == 1) {
            Field child = getField(0);
            layoutChild(child, width, height);
            // adjust manager height.
            height = child.getHeight();
            setPositionChild(child, (width - child.getWidth()) / 2, 0);
        }

        setExtent(width, height);       
    }           
};

mListContainer.add(vfmServiceLists);
add(mListContainer);

【讨论】:

  • 此代码不适用于所有设备,因为我们必须更改每个设备的水平边距。尝试使用标签字段而不是 RichTextField。那么你必须得到解决方案。如果你想在 RichTextField 中只使用 Rupak 代码;
  • 是的,此代码可能无法在所有设备上运行,而且我不是为了解决一般问题而编写的。但是根据显示宽度调整边距,高度并不难。
  • Rupak 和 @alishaik786 感谢您的解决方案... Rupak 的 Last code without any margin 和 RichTextField 似乎是一个完美的解决方案!!!!
【解决方案2】:

最好使用 LabelField 而不是 RichTextField。 如果您只希望 RichTextField 上的结果,则以下代码不适合您;然后尝试使用我们的“Rupak”代码;

以下代码使用 LabelField 而不是 RichTextField

public class Abc extends MainScreen
{
VerticalFieldManager vertical;
Font font=Font.getDefault().derive(Font.BOLD, 18),small_font=Font.getDefault().derive(Font.PLAIN, 15);
LabelField labelField,labelFieldTwo;
public Abc() 
{
    createGUI();
}

private void createGUI() 
{
    vertical=new VerticalFieldManager(USE_ALL_WIDTH);
    vertical.setBackground(BackgroundFactory.createSolidBackground(Color.GREEN));
    HorizontalFieldManager hor=new HorizontalFieldManager(Field.FIELD_HCENTER);     

    hor.add(new LabelField(" We Fix ", Field.NON_FOCUSABLE));
    hor.setFont(font);
    hor.setPadding(5, 0, 5, 0);
    vertical.add(hor);

    HorizontalFieldManager hr=new HorizontalFieldManager(Field.FIELD_HCENTER);

    hr.add(new LabelField("."));        
    hr.add(new LabelField("Computer Science & Laptop", Field.NON_FOCUSABLE));
    hr.setFont(small_font);
    vertical.add(hr);

    HorizontalFieldManager hr1=new HorizontalFieldManager(Field.FIELD_HCENTER);

    hr1.add(new LabelField("."));       
    hr1.add(new LabelField("Modem/Router/Switches", Field.NON_FOCUSABLE));
    hr1.setFont(small_font);
    vertical.add(hr1);

    //------------------- Up to now you want like this; and you can do like this type also like left align and right align;
    HorizontalFieldManager hr2=new HorizontalFieldManager(Field.FIELD_LEFT);

    hr2.add(new LabelField("."));       
    hr2.add(new LabelField("Printer/Scanner", Field.NON_FOCUSABLE));
    hr2.setFont(small_font);
    vertical.add(hr2);

    HorizontalFieldManager hr3=new HorizontalFieldManager(Field.FIELD_LEFT);

    hr3.add(new LabelField("."));       
    hr3.add(new LabelField("Tablet", Field.NON_FOCUSABLE));
    hr3.setFont(small_font);
    vertical.add(hr3);

    HorizontalFieldManager hr4=new HorizontalFieldManager(Field.FIELD_RIGHT);

    hr4.add(new LabelField("."));       
    hr4.add(new LabelField("Printer/Scanner", Field.NON_FOCUSABLE));
    hr4.setFont(small_font);
    vertical.add(hr4);

    HorizontalFieldManager hr5=new HorizontalFieldManager(Field.FIELD_RIGHT);

    hr5.add(new LabelField("."));       
    hr5.add(new LabelField("Tablet", Field.NON_FOCUSABLE));
    hr5.setFont(small_font);
    vertical.add(hr5);

    vertical.setPadding(0, 0, 10, 0);
    add(vertical);
}
}

当字段对齐居中、左右时,我得到了这样的结果:

【讨论】:

  • 我认为您是黑莓新手。为了澄清您和我们的疑问,请访问此聊天室:chat.stackoverflow.com/rooms/4014/…
  • LabelField 没有帮助使字段像图 2 中那样在一条直线上对齐..
【解决方案3】:

试试这个..

LabelField lbl=new LabelField("• Computer & Laptop\r\n• Modem / Router / Switches\r\n• 
Printer / Scanner\r\n• Tablet",LabelField.FIELD_HCENTER);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-12-28
    • 2013-03-30
    • 2015-01-14
    • 2011-01-17
    • 2013-06-12
    • 1970-01-01
    相关资源
    最近更新 更多