在Android中的按钮上写入多行文本

在Android中的按钮上写入多行文本,android,xml,android-widget,android-button,Android,Xml,Android Widget,Android Button,我想知道,如何在按钮上写多行文字 <Button android:id="@+id/button1" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:layout_marginT

我想知道,如何在按钮上写多行文字

 <Button
    android:id="@+id/button1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="100dp"
    android:background="@drawable/layout_border"
    android:text="Hours   Mon - sat 5pm" />

我变得这样:-

但需要这种按钮:-

编辑::--

现在我用@Sino K D:

但仍然在寻求帮助

将drawable添加到左侧后,得到以下结果:-

使用
和#10

(新行)

例如:-

android:text="Hi&#10;Hello"

1) 在../res/values/strings.xml中定义:

 <string name="multilines">Line1Line1\nLine2Line2</string>
Line1Line1\nLine2Line2
2) 在布局文件中引用它:

 <Button
   android:id="@+id/btn_multilines"
   android:text="@string/multilines"
   android:layout_height="wrap_content"
   android:layout_width="fill_parent">
</Button>

使用
&10(新行)


对于Android布局,可以向元素添加多行文本

在res/values/strings.xml中使用字符endOfLine“\n”创建一个新变量

例如:

<string name="multiplelines">Line1 \n Line2</string>
Line1\n第2行
在布局文件中引用它。比如说,

<Button
    android:id="@+id/start"
    android:text="@string/multiplelines"
    android:layout_height="wrap_content"
    android:layout_width="fill_parent">
</Button>

您可以使用
线性布局
而不是按钮来实现类似效果:

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/layout_border"
android:orientation="horizontal" >
    <TextView
    android:layout_width="wrap_content"
    android:layout_height="match_content"
    android:textColor="@color/grey" // you'll have to define this yourself
    android:text="Hours"
    android:gravity="center_vertical" />

    <TextView
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:text="Mon - Sat 5:00pm&#10;Closed on Sundays"
    android:gravity="center_vertical />
</LinearLayout>


您可以使用此工具来实现

1->在布局中创建一个按钮,如下所示

 <Button
        android:id="@+id/buton"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Mon - Sat 5 pm\nClosed on sunday"
        />
3->在活动类中添加这些行

Button button=(Button)findViewById(R.id.button);
button.setCompoundDrawables( new TextDrawable("Hour"), null, null, null);

为什么不试着通过编码来实现呢

String styledText = "<small> <font color='#000000'>"
            + "Mon-Sat 5:00 pm" + "</font> </small>"+ "<br/>"
            + "<small> <font color='#000000'>" + "Closed on Sunday"
            + "</font> </small>";

    sendrequest.setText((Html
            .fromHtml(styledText)));
String styledText=“”
+“周一至周六下午5:00”+“+”
” +“+”周日休息 + " "; sendrequest.setText((Html) .fromHtml(styledText));
可以在strings.xml中使用的解决方案,分隔符是CDATA中的\n

<string name="switch_on_bluetooth"><![CDATA[Allumer\nBluetooth]]></string>

添加


小时是静态文本吗?那么我认为最好设计一个带有文本“小时”的图像,并将其作为按钮的左侧可绘制图标。示例:-android:drawableLeft=“@drawable/hour text”小时也是一个文本而不是可绘制文本,因为我说过你的答案太有用了,我使用了图像,但我想知道如何在左侧添加文本,不管怎样,谢谢你简单而精彩的回答
String styledText = "<small> <font color='#000000'>"
            + "Mon-Sat 5:00 pm" + "</font> </small>"+ "<br/>"
            + "<small> <font color='#000000'>" + "Closed on Sunday"
            + "</font> </small>";

    sendrequest.setText((Html
            .fromHtml(styledText)));
<string name="switch_on_bluetooth"><![CDATA[Allumer\nBluetooth]]></string>
<Button
   android:layout_gravity="center_vertical" 
/>
btn3.setText("Selected Orders\nOnly");