1

背景:J2ME、LWUIT、诺基亚 S40

在过去的几天里,我一直在为此苦苦挣扎。使用 ContainerList 时,无论我如何更改 ContainerList 或元素的 padding、margin 和边框,每个元素周围总是有 2px 的 margin。我把这个示例 midlet 放在一起来展示我在说什么:

import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;

import com.sun.lwuit.Command;
import com.sun.lwuit.Component;
import com.sun.lwuit.Display;
import com.sun.lwuit.Form;
import com.sun.lwuit.Label;
import com.sun.lwuit.events.ActionEvent;
import com.sun.lwuit.layouts.BoxLayout;
import com.sun.lwuit.list.CellRenderer;
import com.sun.lwuit.list.ContainerList;
import com.sun.lwuit.list.DefaultListModel;

public class Test extends MIDlet {

public Test() {

}

protected void startApp() throws MIDletStateChangeException {
    Display.init(this);

    Form form = new Form("ContainerList margin test");

    DefaultListModel model = new DefaultListModel();
    for(int i=0; i<100; i++){
        model.addItem(new String("Element " + i));
    }

    ContainerList list = new ContainerList(new BoxLayout(BoxLayout.Y_AXIS), model);
    list.getStyle().setBgColor(0x00FF00);
    list.getStyle().setBgTransparency(255);
    list.getStyle().setPadding(0,0,0,0);
    list.getStyle().setMargin(0,0,0,0);
    list.getStyle().setBorder(null);

    list.setRenderer(new CellRenderer(){

        public Component getCellRendererComponent(Component plist,
                Object pmodel, Object pvalue, int index, boolean selected) {

            Label l = new Label((String) pvalue);
            l.getStyle().setBgColor(0xff0000);
            l.getStyle().setBgTransparency(255);
            l.getStyle().setPadding(0,0,0,0);
            l.getStyle().setMargin(0,0,0,0);
            l.getStyle().setBorder(null);
            l.getPressedStyle().setBgColor(0x000000);
            l.getPressedStyle().setBgTransparency(255);
            l.getSelectedStyle().setBgColor(0xFFFFFF);
            l.getSelectedStyle().setBgTransparency(255);

            return l;
        }

        public Component getFocusComponent(Component arg0) {
            Label l2 = new Label();
            l2.getStyle().setBgColor(0xFF00FF);
            l2.getStyle().setBgTransparency(255);
            return l2;
        }

    });

    form.addComponent(list);

    Command exitCommand = new Command("Exit") {

        public void actionPerformed(ActionEvent e) {
            notifyDestroyed();
        }
    };
    form.addCommand(exitCommand);

    form.show();
}

protected void destroyApp(boolean arg0) throws MIDletStateChangeException {

}

protected void pauseApp() {

}

}

问题 #1:

如您所见,我删除了列表本身和所有元素上的边距、填充和边框。但是,在诺基亚 s40 模拟器上,我总是看到所有元素周围都有 2px 的边距。

我是否忘记了任何其他应该为此更改的样式?我查看了 getSideGap 和 getBottomGap ,但它们并没有引起问题。

问题 #2:

我们如何在新闻中突出显示元素?设置按下或选择的样式没有区别,返回焦点组件在按下时也没有区别。有一些触摸反馈会很好(我只针对触摸设备,所以焦点不是问题)

4

1 回答 1

0

LWUIT 中的 ContainerList 中的条目之间存在填充错误,该错误已针对Codename One进行了修复。通常,容器列表会创建虚假条目以匹配其中的每个条目,并且它们具有默认的填充/边距。我不知道是否有解决方法,因为我们不再使用 LWUIT,而且我认为没有人认真维护它。

将默认填充/边距设置为 0 可能会解决此问题,但也可能会影响一切。我建议迁移到Codename One作为一种更有效的策略。

于 2013-09-21T15:02:27.140 回答