在Android上的按钮上写多行文字

问题描述 投票:8回答:7

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

 <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后,得到这个: -

android xml android-widget android-button
7个回答
22
投票

使用&#10;

(新队)

例:-

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

要么

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>

2
投票

为什么不通过编码来尝试这一点

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)));

1
投票

你可以实现这个。

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"
        />

2->在项目中添加此类。

import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.ColorFilter;
import android.graphics.Paint;
import android.graphics.PixelFormat;
import android.graphics.drawable.Drawable;

public class TextDrawable extends Drawable {

    private final String text;
    private final Paint paint;

    public TextDrawable(String text) {

        this.text = text;

        this.paint = new Paint();
        paint.setColor(Color.WHITE);
        paint.setTextSize(20f);
        paint.setAntiAlias(true);
        paint.setFakeBoldText(true);
        paint.setShadowLayer(6f, 0, 0, Color.BLACK);
        paint.setStyle(Paint.Style.FILL);
        paint.setTextAlign(Paint.Align.LEFT);
    }

    @Override
    public void draw(Canvas canvas) {
        canvas.drawText(text, 0, 0, paint);
    }

    @Override
    public void setAlpha(int alpha) {
        paint.setAlpha(alpha);
    }

    @Override
    public void setColorFilter(ColorFilter cf) {
        paint.setColorFilter(cf);
    }

    @Override
    public int getOpacity() {
        return PixelFormat.TRANSLUCENT;
    }
}

3->在活动类中添加这些行

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

0
投票

使用&#10;(新行)

android:text="Hours&#10;Mon - sat 5pm"

0
投票

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

在res / values / strings.xml中创建一个带有字符endOfLine“\ n”的新变量。

例如:

<string name="multiplelines">Line1 \n Line2</string>

在布局文件中引用它。例如,

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

0
投票

您可以使用LinearLayout而不是按钮,并实现类似的效果:

<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>

这可能不完美,但它是一个开始


0
投票

可以在strings.xml中使用的解决方案,分隔符是CDATA中的\ n

<string name="switch_on_bluetooth"><![CDATA[Allumer\nBluetooth]]></string>
© www.soinside.com 2019 - 2024. All rights reserved.