Android:粗体按钮文本

发布于 2024-12-02 15:41:21 字数 152 浏览 3 评论 0原文

我的应用程序中有一个按钮。按钮中的文本类似于“Type: Location”。

我想知道是否可以将按钮上的文本更改为“类型:位置”

,即按钮上的部分文本加粗?

感谢您提前抽出时间。

I have a button in my application. the text in the button goes as "Type: Location" something like that.

I'm wondering whether its possible to change the text on the button as "Type: Location"

i.e Bold the text partially on the button??

Thanks for yoru time in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

划一舟意中人 2024-12-09 15:41:21

我们有更好的选择,如下所示:android:textStyle="bold"
android api 支持粗体

we have a more better choice like this :android:textStyle="bold"
android api support bold

单调的奢华 2024-12-09 15:41:21

只需将字符串放入 strings.xml 中并像这样更改它,

 <string name="hello"><b>Hello</b> World, fh!</string>

然后将此文本设置为按钮,如下所示

<Button
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:textAllCaps="false"
    android:text="@string/hello"
    />

有时,当您可能必须使用动态文本时,上述方法不会有帮助。因此,在这种情况下 SpannableString 开始起作用。

  String tempString="Copyright";
  Button button=(Button)findViewById(R.id.button);
  SpannableString spanString = new SpannableString(tempString);
  spanString.setSpan(new StyleSpan(Typeface.BOLD), 0, spanString.length(), 0);
  button.setText(spanString);

Simply put your string in strings.xml and change it like this,

 <string name="hello"><b>Hello</b> World, fh!</string>

and set this text to your button like this

<Button
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:textAllCaps="false"
    android:text="@string/hello"
    />

Sometimes the above approach will not be helpful when you might have to use Dynamic Text. So at that case SpannableString comes into action.

  String tempString="Copyright";
  Button button=(Button)findViewById(R.id.button);
  SpannableString spanString = new SpannableString(tempString);
  spanString.setSpan(new StyleSpan(Typeface.BOLD), 0, spanString.length(), 0);
  button.setText(spanString);
つ低調成傷 2024-12-09 15:41:21

您可以使用 Html.fromHtml() 设置它,并以字符串形式给出带有 HTML 元素的字符串资源。希望这有帮助!

You can set it using Html.fromHtml() and give as a string, a string resource with HTML elements. Hope this helps!

翻身的咸鱼 2024-12-09 15:41:21

使用跨度:

SpannableStringBuilder builder = new SpannableStringBuilder("Type: your type here!");
StyleSpan boldStyle = new StyleSpan(Typeface.BOLD);
builder.setSpan(boldStyle, 0, 5, Spannable.SPAN_INCLUSIVE_INCLUSIVE);
((Button) findViewById(R.id.button)).setText(builder);

Using spans:

SpannableStringBuilder builder = new SpannableStringBuilder("Type: your type here!");
StyleSpan boldStyle = new StyleSpan(Typeface.BOLD);
builder.setSpan(boldStyle, 0, 5, Spannable.SPAN_INCLUSIVE_INCLUSIVE);
((Button) findViewById(R.id.button)).setText(builder);
So尛奶瓶 2024-12-09 15:41:21

您可以在字符串中使用基本标记目录,例如

"Type: Location"

请参阅 使用 HTML 标记设置样式

You can use basic markup directory in strings, e.g.

"<b>Type</b>: Location"

See Styling with HTML markup

刘备忘录 2024-12-09 15:41:21

如果想以编程方式设置文本,请使用此方法

Button

首先,您必须在 XML 中设置按钮的属性

android:textAllCaps="false" // very important without this property might be it won't show effect

public SpannableString setSpanableString(String textString, int start, int end){

    SpannableString spanString = new SpannableString(textString);
    spanString.setSpan(new StyleSpan(Typeface.BOLD), start, end, 0);

    return spanString;
}


Button btn; // get your button reference here

String text = "Hi, Dharmbir";
btn.setText(setSpanableString(text, 4, text.length));// set here your index

TextView

TextView tv; // get your TextView reference here

String text = "Hi, Dharmbir";
tv.setText(setSpanableString(text, 4, text.length));

Output

嗨,Dharmbir< /强>

If want to set text programmatically then use this method

Button

First you have to set button's property in XML

android:textAllCaps="false" // very important without this property might be it won't show effect

public SpannableString setSpanableString(String textString, int start, int end){

    SpannableString spanString = new SpannableString(textString);
    spanString.setSpan(new StyleSpan(Typeface.BOLD), start, end, 0);

    return spanString;
}


Button btn; // get your button reference here

String text = "Hi, Dharmbir";
btn.setText(setSpanableString(text, 4, text.length));// set here your index

TextView

TextView tv; // get your TextView reference here

String text = "Hi, Dharmbir";
tv.setText(setSpanableString(text, 4, text.length));

Output

Hi, Dharmbir

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文