2011-11-29 69 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

在同一個討論主題中檢查我的答案,解決方案是完全瘋了:)非常感謝您的幫助,我將它標記:) –