代号一-修改主题中的字体大小,例如哈希表 [英] Codename One - Modify a font size in a theme like in an hashtable

查看:74
本文介绍了代号一-修改主题中的字体大小,例如哈希表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在上一个问题中代号一个-放大或缩小所有样式中的所有字体的方法我回答了一个方法,该方法可迭代主题的所有值,并且如果给定值是Font的等距形式,它将更改其样式

In the previous question Codename One - Method to enlarge or reduce all the fonts in all the styles I replied with a method that iterates all the values of a theme and, if a given value is an istance of Font, it changes its font size of a given percentage.

问题是它不能按预期工作,因为某些字体已更改,而另一些则没有。例如,如果我的theme.xml中具有以下Label(3.1毫米)的字体大小,则该Label字体大小不会更改:

The problem is that it doesn't work as expected, because some fonts are changed and others are not. For example, if I have the following font size of a Label (3.1mm) in my theme.xml, that Label font size it's not changed:

<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");
    }
}


推荐答案

错误是这一行:

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

正确的代码没有双引号:

The correct code is without the double quotes:

    Hashtable hashtable = theme.getTheme(Theme);

仅此而已:)

这篇关于代号一-修改主题中的字体大小,例如哈希表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆