2012-03-10 94 views
1

我有一個textview,其中我通過java追加幾行,如下所示。android在textview中通過java對齊文本

TextView myTextView = (TextView) findViewById(R.id.mytextview); 
myTextView.append("this is line one of the textview"); 
myTextView.append("this is line two"); 
myTextView.append("this is line three of the textview"); 

我想只居中對齊第二行。我試過不同的html標籤,但都沒有工作。 有人可以告訴我它是如何在android中完成的。

回答

8
myTextView.setGravity(Gravity.CENTER); 
+0

這將對齊中心的整個文本。我只想對齊第二行。 – 2012-03-10 14:46:27

2

HTML標籤的作品,你只需要把它們正確。

myTextView.setText(Html.fromHtml("<center>"+"this is line two"+"</center>")); 

我不知道它是否也適用於append()。試着讓我知道它是否有效。

+0

如果它是唯一的行,那麼它的工作,但如果我之前或之後放置任何東西,那麼它不會。 – 2012-03-11 02:21:02

+0

難道你不能在一個命令中添加整個事物?我做了 – noob 2012-03-11 17:37:39

+0

,但它不起作用... – 2012-03-13 10:53:29