以编程方式为所有组件的默认ttf字体

时间:2018-04-07 14:28:35

标签: java codenameone

我试图以编程方式设置默认字体但似乎没有任何效果。如何以编程方式更改默认字体?

我试过这个:

        def=Font.createTrueTypeFont("Shojumaru", "Shojumaru-Regular.ttf").derive(fis.display_faktor(80), Font.STYLE_PLAIN);
    UIManager.getInstance().getComponentStyle("Label").setFont(def);
    Display.getInstance().getCurrent().refreshTheme();

我也试过这个没有效果:

Hashtable h = new Hashtable();
h.put("font", largeFont);
UIManager.getInstance().addThemeProps(h);
Display.getInstance().getCurrent().refreshTheme();

1 个答案:

答案 0 :(得分:1)

第二种语法应该可以正常工作,但它不会覆盖所有组件,因为如果特定组件定义了本机主题中将被选择的字体。

您还需要使用以下方式覆盖选定/按下/禁用的字体:

h.put("sel#font", largeFont);
h.put("press#font", largeFont);
h.put("dis#font", largeFont);

然后你需要覆盖Button之类的东西,例如:

h.put("Button.font", largeFont);
h.put("Button.sel#font", largeFont);
h.put("Button.press#font", largeFont);
h.put("Button.dis#font", largeFont);

更简单的替代方案是加载基础主题:

Map<String, Object> themeHash = (Map<String, Object>)theme.getTheme("Theme");
for(String k : themeHash.keySet()) {
   if(k.endsWith("font")) {
       themeHash.put(k, largeFont);
   }
}
UIManager.getInstance().setThemeProps(h);
Display.getInstance().getCurrent().refreshTheme();
相关问题