java - 黑莓中央的两个按钮看起来不太好

标签 java blackberry

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

问题是这两个按钮设置不正确。第二个按钮位于第一个按钮的后面,并且与第一个按钮相比,其位图高度也减小了。

对于这两个自定义按钮,我有相同的 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;
    }
} 

enter image description here

最佳答案

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

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

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

关于java - 黑莓中央的两个按钮看起来不太好,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11734249/

相关文章:

blackberry - 声音文件黑莓应用程序的加密/解密

java - out.print() 不适用于 jsp 方法

Java 线程执行受计数和时间限制

java - 将 JSON 从 WebResource 解析为可用的 Java 对象

html - 用于黑莓的嵌入式 HTML 控件?

gwt - 为什么GWT应用程序在Blackberry OS7上崩溃?

java - Spring AMQP AcknowledgeMode.AUTO 工作缓慢

java - Android/Java - 通过在分配之前检测剩余的可用 RAM 来防止 OutOfMemoryError

layout - 方向改变时如何改变布局

java - 黑莓和 sqlite