简体   繁体   English

如何将粗体文本设置为 Android Snackbar 操作文本?

[英]How to set Bold text to Android Snackbar Action Text?

We could set the color for the Action Text of Snackbar using setActionTextColor as documented in https://developer.android.com/reference/android/support/design/widget/Snackbar.html .我们可以使用setActionTextColor设置 Snackbar 的操作文本的颜色,如https://developer.android.com/reference/android/support/design/widget/Snackbar.ZFC35FDC70D5FC69D2569EZ83中所述。 However, is there a way to make the Text BOLD?但是,有没有办法使文本加粗?

Thanks!!谢谢!!

Use snackbar_action Resource ID.使用snackbar_action资源ID。

It turns you that you can use the same method to style the Snackbar's Action text that you use to style the Snackbar's Message text.事实证明,您可以使用与用于设置 Snackbar 消息文本样式的方法相同的方法来设置 Snackbar 操作文本的样式。

You just have to use the Resource ID snackbar_action instead of snackbar_text .您只需要使用资源 ID snackbar_action而不是snackbar_text

Here's an example of setting the style for both the Message text and the Action text.下面是设置样式的消息文本和操作文本两者的例子。

Snackbar snackbar = Snackbar.make( ... ); // Create the Snackbar however you like.

TextView snackbarActionTextView = (TextView) snackbar.getView().findViewById( android.support.design.R.id.snackbar_action );
snackbarActionTextView.setTextSize( 20 );
snackbarActionTextView.setTypeface( snackbarActionTextView.getTypeface(), Typeface.BOLD );

TextView snackbarTextView = (TextView) snackbar.getView().findViewById( android.support.design.R.id.snackbar_text );
snackbarTextView.setTextSize( 16 );
snackbarTextView.setMaxLines( 3 );

In my example, I've set the Action text to have a TextSize of 20 and a bold Typeface , and the Message text to have a TextSize of 16 and allow up to 3 lines.在我的示例中,我将 Action 文本设置为TextSize为 20 和粗体Typeface ,将 Message 文本设置为TextSize为 16 并允许最多 3 行。

The easiest method to add BOLD text to your Snackbar text is to use the Android Html class to generate the text to pass to the Snackbar.make() function.粗体文本添加到 Snackbar 文本的最简单方法是使用Android Html 类生成要传递给 Snackbar.make() 函数的文本。 Here is an example:下面是一个例子:

Snackbar.make(view, Html.fromHtml("Add <b>bold</b> to Snackbar text"), Snackbar.LENGTH_LONG).show();

An alternative method is to use the SpannableStringBuilder class.另一种方法是使用SpannableStringBuilder类。

SpannableStringBuilder snackbarText = new SpannableStringBuilder();
snackbarText.append("Add ");
int boldStart = snackbarText.length();
snackbarText.append("bold");
snackbarText.setSpan(new StyleSpan(android.graphics.Typeface.BOLD), boldStart, snackbarText.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
snackbarText.append(" to Snackbar text");
Snackbar.make(view, snackbarText, Snackbar.LENGTH_LONG).show();

With the version 1.1.0 of the Material Components you can define in your app theme the attribute snackbarButtonStyle :使用Material Components 的1.1.0版本,您可以在应用程序主题中定义属性snackbarButtonStyle

<style name="AppTheme" parent="Theme.MaterialComponents.*">
  <item name="snackbarButtonStyle">@style/snackbar_button</item>
</style>

In this way you can define a custom style for the button used for the Action.通过这种方式,您可以为用于操作的按钮定义自定义样式。
You can change text color, background colore and you can define your custom style for the textAppearance .您可以更改文本颜色、背景颜色,还可以为textAppearance定义自定义样式。
Something like:就像是:

  <style name="snackbar_button" parent="@style/Widget.MaterialComponents.Button.TextButton.Snackbar">
      <item name="android:textAppearance">@style/snackbar_button_textappearance</item>
  </style>

  <style name="snackbar_button_textappearance"  parent="@style/TextAppearance.MaterialComponents.Button">
    <item name="android:textStyle">bold</item>
  </style>

在此处输入图片说明

The best way to bold the text in snackbar is to given in one of the solutions here, but I will make it more simple:在小吃栏中加粗文本的最佳方法是在此处提供一种解决方案,但我将使其更简单:

Snackbar snackbar = Snackbar
                    .make(yourViewHere, Html.fromHtml("<b>Email Copied To Clipboard</b>"), Snackbar.LENGTH_LONG);

 snackbar.show();

The Latest way currently in 2022 is to do this by using HtmlCompat.fromHtml not Html.fromHtml:目前在 2022 年的最新方法是使用HtmlCompat.fromHtml而不是 Html.fromHtml 来做到这一点:

In Java:在 Java 中:

    Snackbar snackbar = Snackbar.make(view, 
                                      Html.fromHtml("<b>Your text here</b>"), 
                                      Snackbar.LENGTH_LONG);
    snackbar.show();

In Kotlin:在 Kotlin 中:

    val snackbar = Snackbar.make(requireView(),
           HtmlCompat.fromHtml("<b>Your text here</b>", HtmlCompat.FROM_HTML_MODE_LEGACY), 
           Snackbar.LENGTH_SHORT)

    snackbar.show()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM