我有一个关于ListField的问题:
如果我调用setSize(16),那么我会指出这个组件包含多少个项目。那我该如何控制这个组件的高度呢?
例如,我使用下面的代码在下面显示一个 TextField 和一个 ListField,但我也想在 ListField 下方显示一个 ButtonField:
MyScreen.java:
private MyList presetList = new MyList();
private MyScreen() {
setTitle("Favorites");
BasicFilteredList filterList = new BasicFilteredList();
String[] days = {"Monday","Tuesday","Wednesday",
"Thursday","Friday","Saturday","Sunday"};
int uniqueID = 0;
filterList.addDataSet(uniqueID,days,"days",BasicFilteredList.COMPARISON_IGNORE_CASE);
AutoCompleteField autoCompleteField = new AutoCompleteField(filterList);
add(autoCompleteField);
presetListField.setRowHeight(-2);
presetListField.setCallback(presetList);
presetListField.setSize(MyList.items.length());
add(presetListField);
}
MyList.java:
public class MyList implements ListFieldCallback {
public static String items[] = {
"Favorite 01", "Favorite 02", "Favorite 03", "Favorite 04",
"Favorite 11", "Favorite 12", "Favorite 13", "Favorite 14",
"Favorite 21", "Favorite 22", "Favorite 23", "Favorite 24",
"Favorite 31", "Favorite 32", "Favorite 33", "Favorite 34"
};
public void drawListRow(ListField listField, Graphics g, int index, int y, int width) {
Font f = g.getFont();
Font b = f.derive(Font.BOLD, f.getHeight() * 2);
g.setColor(Color.WHITE);
g.drawText(items[index], Display.getWidth()/3, y);
g.drawText("Click to get frequency", Display.getWidth()/3, y + g.getFont().getHeight());
g.setFont(b);
g.setColor(Color.YELLOW);
g.drawText("100." + index, 0, y);
}
public Object get(ListField list, int index) {
return items[index];
}
public int indexOfList(ListField list, String p, int s) {
return -1;
}
public int getPreferredWidth(ListField list) {
return Display.getWidth();
}
}
谢谢!亚历克斯
更新:
也许 ListField 应该始终显示其所有项目,而您不能将其视图限制为几行?