1

I would like to display white spaces before and after text on a Button. For example :

 "    A    "

However when I set the text above to a Button :

myButton.setText ( "    A    " );

The text is trimmed (only trailing spaces are removed), and the following text is displayed :

"    A"

How can I display the text without having it trimmed ?

4

3 回答 3

2

使用Html.fromHtml

例如

利用btn.setText(Html.fromHtml("   A  "));

于 2013-06-10T11:23:29.733 回答
0

使用 XML 编码 \u0020用于空格。

<string name="your_id">&#160; String &#160; </string>

<string name="your_id">\u0020 String \u0020</string>
于 2013-06-10T11:24:35.000 回答
0

除了一些遗留系统之外,尾随空格自然会被这样处理。

如果原因只是将按钮宽度设置为某个大小,为什么不在 XML 布局中使用以下内容:

android:layout_width="130dp" // Set the width according to needs in pixels

或在您的代码中如下所示:

mybuttonn.setWidth(50); // Set the width according to needs in pixels

希望这可以帮助。

于 2013-06-10T11:27:04.590 回答