如何在android中设置带有文本的按钮

如何在android中设置带有文本的按钮,android,arrays,button,text,Android,Arrays,Button,Text,我有个问题。可以制作一个有3行文本的按钮吗。这是我的想法 |---------------| |text1... | |text2... | |text3... | |_______________| 但是我想放在按钮上的文本是从一个数组接收的,它会动态变化。这是可行的还是有更容易的替代方法?如果您试图在布局XML文件中添加新行: 使用和#10(新行) 如果您试图在代码中添加新行,只需使用“\n”,与在任何其他文本中相同 如果你看不到第二行,可能是你的按钮没

我有个问题。可以制作一个有3行文本的按钮吗。这是我的想法

|---------------|
|text1...       |
|text2...       |
|text3...       |
|_______________|

但是我想放在按钮上的文本是从一个数组接收的,它会动态变化。这是可行的还是有更容易的替代方法?

如果您试图在布局XML文件中添加新行:

使用
和#10(新行)


如果您试图在代码中添加新行,只需使用“\n”,与在任何其他文本中相同

如果你看不到第二行,可能是你的按钮没有足够的高度。例如,在我的例子中,包含按钮的布局有一个固定的高度,恰好使我的按钮完美地显示了一行文本

第二种方法:

button.setText("Line1\nLine2\nline3");  //button is the Button of id "btn_multilines"
1) 在../res/values/strings.xml中定义:

<string name="multilines">Line1Line1\nLine2Line2</string>

如果试图在布局XML文件中添加新行:

使用
和#10(新行)


如果您试图在代码中添加新行,只需使用“\n”,与在任何其他文本中相同

如果你看不到第二行,可能是你的按钮没有足够的高度。例如,在我的例子中,包含按钮的布局有一个固定的高度,恰好使我的按钮完美地显示了一行文本

第二种方法:

button.setText("Line1\nLine2\nline3");  //button is the Button of id "btn_multilines"
1) 在../res/values/strings.xml中定义:

<string name="multilines">Line1Line1\nLine2Line2</string>
在按钮文本中使用“\n”创建新行

<Button 
        android:text="text1\ntext2\ntext3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

这里看起来像

在按钮文本中使用“\n”创建新行

<Button 
        android:text="text1\ntext2\ntext3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

这里看起来像


您可以按如下编程方式设置按钮上的文本:

btn.setText("Text1\n"+"Text2\n"+"Text3");
<Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Text1\nText2\nText3"/>
也可以直接在xml中设置,如下所示:

btn.setText("Text1\n"+"Text2\n"+"Text3");
<Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Text1\nText2\nText3"/>

您可以按如下编程方式设置按钮上的文本:

btn.setText("Text1\n"+"Text2\n"+"Text3");
<Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Text1\nText2\nText3"/>
也可以直接在xml中设置,如下所示:

btn.setText("Text1\n"+"Text2\n"+"Text3");
<Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Text1\nText2\nText3"/>