2012-05-10 47 views
0

我對黑莓手機非常陌生,試圖製作一個自定義管理器。所以我試圖讓佈局非常簡單,它會顯示兩個Labelfields(我可以非常容易地將它們都放到Horizo​​ntalFieldManager中,但我需要使用自定義管理器來完成此操作。)設置自定義管理器中的字段寬度

Out out should是這樣的: - Hello(FirstField) User(SecondField)

這裏是我的課堂我sublayout方法,擴展到經理

public MyManager() { 
    // construct a manager with vertical scrolling 
    super(Manager.VERTICAL_SCROLL); 
    } 

protected void sublayout(int width, int height) { 
     Field field; 
     // get total number of fields within this manager 
     int numberOfFields = getFieldCount(); 
     int x = 0; 
     int y = 0; 
     for (int i = 0; i < numberOfFields; i++) { 
      field = getField(i); // get the field 
      setPositionChild(field, x, y); // set the position for the field 
      layoutChild(field, width, height); // lay out the field 
      x = x + field.getHeight(); 
     } 
     setExtent(width, height); 
    } 

如果我刪除此行x = x+field.getWidth();然後兩種文本(你好用戶)將被重疊 (我認爲這是因爲x,y = 0)現在我希望我會得到field.getWidth()將返回字段一使用的寬度,但它給我的顯示寬度(這是我的想法),所以我只能在佈局中看到Hello。

對於垂直定位項目,它可以正常使用y = y+field,getHeight();,但不知道爲什麼getWidth沒有返回我適當的寬度值,可能是因爲我誤導某處以理解此問題。

我需要重寫getPrefferedWidth()方法?我也嘗試過這種方法,並保持原樣,但是在兩個字段和其他文本之間留下少量空格(2-3)。

回答

1

更新:我添加了基於你的問題,更新前我的回答滿刻度的例子(我已經採取了sublayout()方法仍然只是,我已經失蹤y變量定義相同添加)。

當你重寫sublayout,你應該首先佈局的領域,然後才放置它們。試試這個代碼:

public final class HelloUserScreen extends MainScreen { 
    public HelloUserScreen() {   
     Manager customManager = new Manager(0) { 
      protected void sublayout(int width, int height) { 
       Field field; 
       int numberOfFields = getFieldCount(); 

       int widthUsed = 0; 
       int maxHeight = 0; 
       int y = 0; 

       for (int i = 0; i < numberOfFields; i++) { 
        field = getField(i); // get the field 

        // first layout 
        layoutChild(field, width-widthUsed, height); 

        // then position 
        setPositionChild(field, widthUsed, y); 

        widthUsed += field.getWidth(); 
        maxHeight = Math.max(maxHeight, field.getHeight()); 
       } 

       setExtent(widthUsed, maxHeight); 
      } 
     }; 

     LabelField helloLabel = new LabelField("Hello "); 
     helloLabel.setBackground(BackgroundFactory.createSolidBackground(Color.GREEN)); 
     customManager.add(helloLabel); 

     LabelField userLabel = new LabelField("user"); 
     userLabel.setBackground(BackgroundFactory.createSolidBackground(Color.YELLOW)); 
     customManager.add(userLabel); 

     add(customManager); 
    } 
} 

此代碼將產生以下屏幕

enter image description here

你應該考慮到,布點一些領域後,可用的寬度和高度留給布點變小(在你的的情況下,因爲你正在水平佈局你的領域,主要是的寬度問題)。

另一件事是,你要調用的setExtent()方法與寬度和高度,你實際使用的布點你的領域,而不是在sublayout()收到(除非你是有意這樣做是因爲一些具體的UI佈局的最大寬度和高度邏輯)。

+0

我認爲y將在setPositionChild方法中是maxHeight。我試過了,但是它在整個屏幕上仍然只顯示出一個元素。 –

+0

@ R.j。你只在這裏發佈了一部分代碼。字段的定義丟失 - 發佈。您描述的行爲可能是由使用所有可用寬度的字段造成的。在任何情況下,正如我在答覆中所說的那樣 - 您應該首先佈置這些字段,而不僅僅是定位它們(​​否則,您將在它們的寬度和高度尚未確定之前嘗試對它們進行定位)。 – mrvincenzo

+0

嗨,我只在我的自定義管理器類中重寫此方法,並有一個構造函數,我編輯了我的代碼。 –

0

您不需要這裏的自定義字段。在那裏僅使用HorizontalFieldManager並且覆蓋getPreferredWidth()來代替Fieldreturn getScreen().getWidth()/2

+0

你好尤金,是的,我可以很容易地通過水平經理實現這一點,但我要實現自定義的經理,我發現這個例子很容易實現,並知道我可以在黑莓實現定製設計所以這就是爲什麼我這樣做通過擴展經理類。 –

相關問題