代码之家  ›  专栏  ›  技术社区  ›  Francesco Galgani

Codename One-在类似哈希表的主题中修改字体大小

  •  1
  • Francesco Galgani  · 技术社区  · 7 年前

    在上一个问题中 Codename One - Method to enlarge or reduce all the fonts in all the styles 我用一种方法来回答,该方法迭代一个主题的所有值,如果给定的值是字体的距离,它会将其字体大小更改为给定的百分比。

    问题是它没有按预期工作,因为一些字体已更改,而其他字体未更改。例如,如果我的主题中有以下字体大小的标签(3.1mm)。xml,标签字体大小不变:

    <font key="Label.font" type="ttf" face="0" style="0" size="0" name="native:MainThin" family="native:MainThin" sizeSettings="3" actualSize="3.1" />
    

    /**
     * Increases or reduces of a given percentage all the font sizes of a given
     * theme, overlaying a new theme containing only the new font sizes. The
     * font sizes will be increased when the percentage is positive, they will
     * be reduced when the percentage is negative. Legal percentage values are
     * from -90 to 300.
     *
     * Example of usage: changeFontSizesOfTheme(theme, "Theme", 50);
     *
     * @param theme the given resource file
     * @param themeName the name of given theme
     * @param percentage the given percentage to increase or reduce the font
     * sizes
     */
    public static void changeFontSizesOfTheme(Resources theme, String themeName, int percentage) {
        if (theme.isTheme(themeName) && percentage != 0 && percentage >= -90 && percentage <= 300) {
            Hashtable hashtable = theme.getTheme("Theme");
            Hashtable overlay = new Hashtable();
            Set<String> keys = hashtable.keySet();
            Iterator<String> itr = keys.iterator();
            String key;
            Object value;
            int count = 0;
            while (itr.hasNext()) {
                key = itr.next();
                value = hashtable.get(key);
                if (value instanceof Font) {
                    Font originalFont = (Font) value;
                    float originalFontSize = originalFont.getPixelSize();
                    float newFontSize = originalFontSize * (100 + percentage) / 100;
                    overlay.put(key, originalFont.derive(newFontSize, originalFont.getStyle()));
                    count++;
                }
            }
            UIManager.getInstance().addThemeProps(overlay);
            Log.p(count + " font sizes of the theme \"" + themeName + "\" were overlayed");
        }
    }
    
    1 回复  |  直到 7 年前
        1
  •  0
  •   Francesco Galgani    7 年前

    错误是行:

        Hashtable hashtable = theme.getTheme("Theme");
    

        Hashtable hashtable = theme.getTheme(Theme);