GridFieldManager不会跨越整个屏幕宽度

时间:2010-10-19 04:05:13

标签: blackberry grid fieldmanager

好的,我正在为黑莓Bold 9700开发,我正试图获得一个1X4网格(1行,4列)来跨越黑莓屏幕的整个宽度,但它一直在变短。我的意思是,默认情况下网格是左对齐的,如果我可以让整个网格跨越整个宽度(这没关系),这很好。有些开发人员可以告诉我我做错了什么吗?我以为你只是在声明一个新网格时在构造函数中添加了GridFieldManager.USE_ALL_WIDTH,但它仍然不适用于我。

final class App3_MainScreen extends MainScreen {
private int numColumns, size;
// Constructor
App3_MainScreen() {
    // declare a layout manager to take care of all the layout stuff
    numColumns = 4;
    size = 4;

    VerticalFieldManager vfm = new VerticalFieldManager();
    vfm.add(new LabelField("using all width & long label...", LabelField.ELLIPSIS | Field.FIELD_HCENTER));

    int borderHeight = Display.getHeight()/2;g
    int borderWidth = Display.getWidth()/2;

    Manager gridFieldManager = new GridFieldManager(1, 4, GridFieldManager.USE_ALL_WIDTH | GridFieldManager.AUTO_SIZE);    // 1 row and 4 columns
    gridFieldManager.add(new ButtonField(""+borderHeight, Field.FIELD_HCENTER));
    gridFieldManager.add(new ButtonField("222", Field.FIELD_HCENTER));
    gridFieldManager.add(new ButtonField("333", Field.FIELD_HCENTER));
    gridFieldManager.add(new ButtonField(""+borderWidth, Field.FIELD_RIGHT));

    // set padding around each buttonField - top=0, right=5, bottom=0, left=5
    gridFieldManager.setPadding(0, 5, 0, 5);
    int gfmHeight = 48 * (size / numColumns);
    gridFieldManager.setBorder(BorderFactory.createSimpleBorder(
            new XYEdges(borderHeight/10, 0, borderHeight/10, 0), // top, right, bottom, left
            Border.STYLE_DASHED));

    add(gridFieldManager);
}}

1 个答案:

答案 0 :(得分:10)

我在下面提供了一个可以解决这个问题的例子。它基于您提供的原始代码,但为了清晰起见,清理并通用。

基本上,GridFieldManager不显式支持USE_ALL_WIDTH。作为Manager,它继承了这个常量,但是它的文档并没有表明它是一个受支持的状态。最好的办法是依靠FIXED_SIZE状态,并根据显示的大小(displayWidth / numColumns)计算每列的宽度。然后,您可以使用GridFieldManager#setColumnProperty()来定义列的固定宽度。

请务必考虑应用于列的填充,然后您就可以了。

希望这有帮助。

/**
 * Shows an example implementation of how to have a GridFieldManager
 * sized to the width of the Display.
 */
final class ScreenWidthGridExample extends MainScreen
{
    /**
     * Number of rows in the grid.
     */
    private static final int NUM_ROWS = 1;

    /**
     * Number of columns in the grid.
     */
    private static final int NUM_COLUMNS = 4;

    /**
     * The grid's column padding.
     */
    private static final int COLUMN_PADDING = 5;

    /**
     * Toggle switch to show the border around the grid.
     */
    private static final boolean SHOW_BORDER = true;

    /**
     * Allocated a new instance of the ScreenWidthGridExample.
     */
    ScreenWidthGridExample() {
        // Set up the GridFieldManager
        GridFieldManager gfm =
                new GridFieldManager(NUM_ROWS, NUM_COLUMNS, 
                GridFieldManager.FIXED_SIZE);
        gfm.setColumnPadding(COLUMN_PADDING);
        if(SHOW_BORDER) {
            gfm.setBorder(BorderFactory.createSimpleBorder(
                    new XYEdges(0, 0, 0, 0), // top, right, bottom, left
                    Border.STYLE_DASHED));
        }
        add(gfm);

        // Size the columns of the GridFieldManager. Make sure to calculate
        // for the padding applied to the columns.
        int columnWidth = (Display.getWidth() / NUM_COLUMNS) - 
                gfm.getColumnPadding();
        for(int i = 0; i < NUM_COLUMNS; i++) {
            gfm.setColumnProperty(i, GridFieldManager.FIXED_SIZE, columnWidth);
        }

        // Populate the columns.
        gfm.add(new ButtonField("1", Field.FIELD_HCENTER));
        gfm.add(new ButtonField("2", Field.FIELD_HCENTER));
        gfm.add(new ButtonField("3", Field.FIELD_HCENTER));
        gfm.add(new ButtonField("4", Field.FIELD_HCENTER));
    }
}