[英]How to set a button with a text in android
我有個問題。 是否可以使按鈕具有3行文本。 這是我的主意
|---------------|
|text1... |
|text2... |
|text3... |
|_______________|
但是我要放在按鈕上的文本是從數組接收的,並且它是動態變化的。 這是可行的,還是有其他更容易做到的選擇?
在按鈕文本中使用“ \\ n”創建新行
<Button
android:text="text1\ntext2\ntext3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
在這里看起來像
如果要在布局XML文件中添加新行:
使用
(新隊)
android:text="Hi Hello"
如果您要在代碼中添加新行,請使用“ \\ n”,與其他任何文本相同。
如果看不到第二行,則可能是您的按鈕沒有足夠的高度。 IE,就我而言,包含按鈕的布局具有固定的高度,剛好使我的按鈕完美顯示一行文本。
第二種方法:
1)在../res/values/strings.xml中定義:
<string name="multilines">Line1Line1\nLine2Line2</string>
2)在布局文件中引用它:
<Button
android:id="@+id/btn_multilines"
android:text="@string/multilines"
android:layout_height="wrap_content"
android:layout_width="fill_parent">
</Button>
動態地:
button.setText("Line1\nLine2\nline3"); //button is the Button of id "btn_multilines"
您可以像這樣通過編程方式在按鈕上設置文本:
btn.setText("Text1\n"+"Text2\n"+"Text3");
或者您可以像這樣直接在xml中進行設置:
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Text1\nText2\nText3"/>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.