2011-11-29 116 views
2

我想将右侧的TextArea中的文本对齐。我尝试下面的代码:如何对齐LWUIT中TextArea的内容?

 Form form = new Form(); 
    TextArea textArea = new TextArea("Some Arabic text ..."); 
    textArea.setRTL(true); 
    textArea.setAlignment(RIGHT); 
    form.addComponent(textArea); 


换来的只是移动滚动到左,
但文本尚未对齐RIGHT
检查下面的图片:

enter image description here

那么如何将内容与RIGHT对齐呢?

回答

2

这听起来很疯狂一审:)但对齐设置为TextArea.LEFT解决问题“TextArea.RIGHT”,现在是RIGHT对齐!

Form form = new Form(); 
    TextArea textArea = new TextArea("Some Arabic text ..."); 
    textArea.setRTL(true); 
    textArea.setAlignment(TextArea.LEFT); 
    form.addComponent(textArea); 

将其设置为LEFT,使显示的文本对齐RIGHT

或者通过移除textArea.setRTL(true)这是镜像显示

Form form = new Form(); 
    TextArea textArea = new TextArea("Some Arabic text ..."); 
    textArea.setAlignment(TextArea.RIGHT); 
    form.addComponent(textArea); 



对于那些当它被设置为RTL谁感兴趣的更复杂的细节
paint方法TextArea类是

public void paint(Graphics g) { 
    UIManager.getInstance().getLookAndFeel().drawTextArea(g, this); 
} 

而在DefaultLookAndFeeldrawTextArea方法如下:

int align = ta.getAbsoluteAlignment(); 
// remaining code is here in initial source 
switch(align) { 
    case Component.RIGHT: 
      x = ta.getX() + ta.getWidth() - rightPadding - f.stringWidth(displayText); 
      break; 
    // remaining code is here in initial source 
} 
g.drawString(displayText, x, y); 

不幸TextArea.RIGHT值为3
但调用ta.getAbsoluteAlignment()当它返回1(尽管该对象的对准是通过代码设定到TextArea.RIGHT !!)
同时TextArea.Left值为1
这就是为什么它匹配t他在开关量,并对准RIGHT

顺便说一句,如果你设置

textArea.setAlignment(Component.RIGHT); 

这也将是错误的,因为Component.RIGHT paint方法外面的值是3而不是1!

+2

当您为'TextArea'激活** RTL **时,文本渲染的方向将反转,因此Left被视为正确。我建议你通过填写@ [LWUIT Java表单](http://www.java.net/forums/mobile-embedded/lwuit)来从Shai得到澄清。从而为LWUIT社区做出贡献。 – Vimal

+1

Vimal是正确的。这听起来很奇怪,但一旦你想到它就很有意义。当你激活bidi时,你有效地倒转了显示器,所以右边是左边,东边是西边...这对于RTL应用程序来说是有意义的,因为在这些应用程序中,99%的UI组件要与英文版完全相反,因此您可以编写英文用户界面,并通过翻译和翻转双向交换机将其本地化为bidi。 –

+0

是的,它可以通过删除我的初始文章中的这一行代码来完成:** textArea.setRTL(true); **和保持原样。感谢这宝贵的意见! –

1

你只需要编写,而不是“正确”

textArea.setAlignment(TextArea.RIGHT); 
+0

我已经尝试TextArea.RIGHT,不幸的是仍然文本左对齐(顺便说一句,即使文本是英文,它左对齐)! –

+0

我试着用你的代码和文本对齐! – frayab

+0

在同一个讨论主题中检查我的答案,解决方案是完全疯了:)非常感谢您的帮助,我将它标记:) –