How to set Bold text to Android Snackbar Action Text?

青春壹個敷衍的年華 提交于 2019-12-19 07:11:34

问题


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. However, is there a way to make the Text BOLD?

Thanks!!


回答1:


Use snackbar_action Resource 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.

You just have to use the Resource ID snackbar_action instead of 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.




回答2:


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



回答3:


With the version 1.1.0 of the Material Components you can define in your app theme the attribute 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.
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>



来源:https://stackoverflow.com/questions/32225684/how-to-set-bold-text-to-android-snackbar-action-text

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!