在 Java ME 中,如何更改 MIDlet 显示内容的方向,使其从右向左?

发布于 2025-01-06 21:52:42 字数 297 浏览 1 评论 0原文

我的 Java ME MIDlet 允许用户更改 Midlet 的语言。
我的代码处理国际化,对于从左到右的语言来说它工作得很好。
但是,当用户将语言更改为从右到左的语言时,会显示正确的字符串,但屏幕仍保持左对齐。

换句话说,手机的区域设置是 en_US,我不想更改它。
我只想更改我的 MIDlet 的区域设置。
动态更改 MIDlet 的所有屏幕以右对齐其内容的最简单方法是什么?
我不介意解决方案是否涉及让用户重新启动应用程序。
如果没有 Java ME 解决方案,我也不介意该解决方案是否是诺基亚手机专有的。

My Java ME MIDlet allows its user to change the language of the Midlet.
My code handles the internationalization, and it works fine for left-to-right languages.
But when the user changes the language to Right-To-Left language, the correct strings are being displayed but the screens remain left-justified.

In other words, the phone's locale is en_US and I don't want to change it.
I just want to change my MIDlet's locale.
What's the simplest way to dynamically change all the screens of the MIDlet to right-justify their content?
I don't mind if the solution involves having the user restart the application.
I also don't mind if the solution is proprietary to Nokia phones if there is no Java ME solution.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

南笙 2025-01-13 21:52:42

假设您使用的是 LCDUI,默认情况下,StringItems 等将根据手机的区域设置获得默认布局。即 Item.LAYOUT_DEFAULT,这意味着具有 en_US 区域设置的手机将从左到右显示项目,而具有 ar_EG 区域设置的手机将显示文本从右到左。

但是,可以使用 setLayout() 函数强制布局右对齐文本:

StringItem myStringItem  = new StringItem("Title", "The text I want to display", Item.PLAIN);
myStringItem.setLayout(Item.LAYOUT_RIGHT);
append(myStringItem );

您可以轻松创建一个单例 Settings 类,该类可以保存一个带有用于对齐的值的标志(Item.LAYOUT_LEFTItem.LAYOUT_RIGHT),并在设置布局时调用它,例如:

myStringItem.setLayout(Settings.getInstance().getJustification());

这也可以在构造函数如果你 希望。

对于低级Graphics,可以使用drawString()方法并更改文本的方向,但您需要从右上角计算起点文本不是左上角

  if (Settings.getInstance().getJustification() != Item.LAYOUT_RIGHT ) {
       g.drawString("Some Text", x + TEXT_MARGIN , y ,Graphics.TOP | Graphics.LEFT);
  } else {
       // Arabic rendering of menu items - getWidth() is the maximum length
       // of the line 
       g.drawString("Some Arabic Text",  x + getWidth() - TEXT_MARGIN, y ,
                            Graphics.TOP | Graphics.RIGHT);
  }

最简单的解决方案(您已经拒绝了)是在整个过程中使用 Item.LAYOUT_DEFAULT 并更改手机的区域设置(当然),但您仍然需要使用覆盖drawString() 如果您使用低级图形。

为了检查正确的理由,我将使用 System.getProperty("microedition.locale") 将输入区域设置输入到如下函数中:

 static final String[] RIGHT_TO_LEFT = {
        "ar", // Arabic
        "az", // Azerbaijani
        "he", // Hebrew
        "jv", // Javanese
        "ks", // Kashmiri
        "ml", // Malayalam
        "ms", // Malay
        "pa", // Panjabi
        "fa", // Persian
        "ps", // Pushto
        "sd", // Sindhi
        "so", // Somali
        "tk", // Turkmen
        "ug", // Uighur
        "ur", // Urdu
        "yi" // Yiddish
    };

 public static int getJustification(String locale) {

        for (int index = 0; index < RIGHT_TO_LEFT.length; index++) {
            if (locale.indexOf(RIGHT_TO_LEFT[index]) != -1) {
                return Item.LAYOUT_RIGHT;
            }
        }
        return Item.LAYOUT_DEFAULT;
    }

Assuming you are using LCDUI, by default StringItems and so on will be given a default layout based on the phone's locale. i.e. Item.LAYOUT_DEFAULT, this means a phone with an en_US locale will display items left to right whereas a phone with an ar_EG locale will display texts right to left.

It is however possible to force the layout to right justify texts using the setLayout() function:

StringItem myStringItem  = new StringItem("Title", "The text I want to display", Item.PLAIN);
myStringItem.setLayout(Item.LAYOUT_RIGHT);
append(myStringItem );

You could easily create a singleton Settings class, which could hold a flag with the value to use for justification ( Item.LAYOUT_LEFT or Item.LAYOUT_RIGHT), and call it when setting the layout e.g.:

myStringItem.setLayout(Settings.getInstance().getJustification());

This could also be done in the constructor if you wish.

For low level Graphics the drawString() method can be used and the direction of the text altered, but you'll need to calculate the start point from the top right of your text not the top left

  if (Settings.getInstance().getJustification() != Item.LAYOUT_RIGHT ) {
       g.drawString("Some Text", x + TEXT_MARGIN , y ,Graphics.TOP | Graphics.LEFT);
  } else {
       // Arabic rendering of menu items - getWidth() is the maximum length
       // of the line 
       g.drawString("Some Arabic Text",  x + getWidth() - TEXT_MARGIN, y ,
                            Graphics.TOP | Graphics.RIGHT);
  }

The simplest solution (which you have already rejected) would be to use Item.LAYOUT_DEFAULT throughout and alter the locale of the phone (of course), but you will still need to use an override for drawString() if you use low level graphics.

To check the correct justification I would enter the input locale using System.getProperty("microedition.locale") into a function such as this:

 static final String[] RIGHT_TO_LEFT = {
        "ar", // Arabic
        "az", // Azerbaijani
        "he", // Hebrew
        "jv", // Javanese
        "ks", // Kashmiri
        "ml", // Malayalam
        "ms", // Malay
        "pa", // Panjabi
        "fa", // Persian
        "ps", // Pushto
        "sd", // Sindhi
        "so", // Somali
        "tk", // Turkmen
        "ug", // Uighur
        "ur", // Urdu
        "yi" // Yiddish
    };

 public static int getJustification(String locale) {

        for (int index = 0; index < RIGHT_TO_LEFT.length; index++) {
            if (locale.indexOf(RIGHT_TO_LEFT[index]) != -1) {
                return Item.LAYOUT_RIGHT;
            }
        }
        return Item.LAYOUT_DEFAULT;
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文