0

我正在开发一个应用程序,我想在其中创建两个按钮。这些按钮应center与位图填充背景对齐自定义按钮。每个还应包含以该按钮为中心的文本。

问题是这两个按钮设置不正确。第二个按钮在第一个按钮之后,它的位图高度也比第一个按钮减小。

对于两个自定义按钮,我有相同的 CustomButton 类。

这是代码:

 CustomButtonField aboutM1 = new CustomButtonField(0,"About G1",registerbg,registerbg,Field.FOCUSABLE,0x324F85);
                    add(new RichTextField(Field.NON_FOCUSABLE));

                  //  CustomButtonField2 ForgotPass = new CustomButtonField2("Forgot Password?",0x324F85);
                    CustomButtonField ForgotPass = new CustomButtonField(0,"Forgot Password?",registerbg,registerbg,Field.FOCUSABLE,0x324F85);

                    add(new RichTextField(Field.NON_FOCUSABLE));

                    VerticalFieldManager bottomVFM = new VerticalFieldManager(USE_ALL_WIDTH);
                    HorizontalFieldManager bottomHFM = new HorizontalFieldManager(FIELD_HCENTER);

                    bottomHFM.add(aboutM1);
                    bottomHFM.add(ForgotPass);
                    bottomVFM.add(bottomHFM);
                    add(bottomVFM);

自定义按钮:

import net.rim.device.api.system.Bitmap;
import net.rim.device.api.ui.*;

public class CustomButtonField extends Field
{
    Bitmap Unfocus_img, Focus_img, current_pic;
    int width;
    String text;
    Font font;   
    int custColor;
    CustomButtonField(int width, String text, Bitmap onFocus, Bitmap onUnfocus, long style,int custColor)
    {
        super(style);
        Unfocus_img = onUnfocus;
        Focus_img = onFocus;
        current_pic = onFocus;
        this.text = text;
        this.width = width;
        this.custColor = custColor;
    }
    protected void layout(int width, int height) 
    {
        setExtent(current_pic.getWidth(), current_pic.getHeight());        
    }
    protected void paint(Graphics graphics) 
    {
        try
        {
                FontFamily fntFamily = FontFamily.forName("BBAlpha Sans");
                font = fntFamily.getFont(Font.BOLD,20);              
        }
        catch(Exception e)
        {
            font = Font.getDefault();

        }
        graphics.setFont(font);


        graphics.setColor(custColor); 

        int xText = (getWidth() - font.getAdvance(text)) / 2;
        int yText = (getHeight() - font.getHeight()) / 2;

        graphics.drawBitmap(0, 0, current_pic.getWidth(), current_pic.getHeight(), current_pic , 0 , 0);
        graphics.drawText(text, xText, yText);

       /* graphics.drawBitmap(0, 0, current_pic.getWidth(), current_pic.getHeight(), current_pic , 0 , 0);
        graphics.drawText(text, width , 7);*/
        graphics.setDrawingStyle(Graphics.HCENTER | Graphics.VCENTER, true);
    }
    protected void onFocus(int direction) 
    {
        super.onFocus(direction);
        current_pic = Unfocus_img;
        this.invalidate();
    }
  protected void drawFocus(Graphics graphics, boolean on) 
  {

    }
    protected void onUnfocus() 
    {
        super.onUnfocus();
        current_pic = Focus_img;
        invalidate();
    }
    public boolean isFocusable() {
        return true;
    }
    protected boolean navigationClick(int status, int time) {
        fieldChangeNotify(0);
        return true;
    }
} 

在此处输入图像描述

4

1 回答 1

1

您使用的代码是正确的,唯一的缺点是您根据位图宽度和文本长度将文本居中,而不是位图宽度。你可能需要改变你的方法。在下面的 URL 中查看 Blackberry UI 示例并检查 EmbossedButtonField Demo

https://github.com/blackberry/Samples-for-Java

一旦我们不确定按钮标签的长度,它是创建自定义按钮的好方法。

于 2012-08-01T06:17:40.370 回答