Android Snackbar TextAlignment in Center

2020-07-02 06:26发布

enter image description here

How to change the Snackbar text alignment to center ? bellow code is not working

Snackbar snack = Snackbar.make(findViewById(android.R.id.content), intent.getStringExtra(KEY_ERROR_MESSAGE), Snackbar.LENGTH_LONG);
View view = snack.getView();
TextView tv = (TextView) view.findViewById(android.support.design.R.id.snackbar_text);
tv.setTextColor(ContextCompat.getColor(LoginActivity.this, R.color.red_EC1C24));
tv.setTextAlignment(View.TEXT_ALIGNMENT_CENTER);
snack.show();

标签: android
6条回答
Fickle 薄情
2楼-- · 2020-07-02 06:41

Try following code it's work for set snackbar text in center:

    Snackbar snackbar = Snackbar.make(main_layout, "This is snack", Snackbar.LENGTH_LONG);
    View snackbarView = snackbar.getView();
    Snackbar.SnackbarLayout layout = (Snackbar.SnackbarLayout) snackbar.getView();
    layout.setGravity(Gravity.CENTER);
    TextView mTextView = (TextView) snackbarView.findViewById(android.support.design.R.id.snackbar_text);
    mTextView.setGravity(Gravity.CENTER_HORIZONTAL);
    mTextView.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT));
    snackbar.show();
查看更多
Bombasti
3楼-- · 2020-07-02 06:42
tv.setGravity(Gravity.CENTER_HORIZONTAL);

EDIT

The appearance of Snackbar was changed in Support library v23 so the correct answer now is:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M){
    tv.setTextAlignment(View.TEXT_ALIGNMENT_CENTER);
} else {
    tv.setGravity(Gravity.CENTER_HORIZONTAL);
}
查看更多
\"骚年 ilove
4楼-- · 2020-07-02 06:51

Another solution, using XML:

  • Your main theme has to inherit from Theme.MaterialComponents, and has to delcare its own styling for Snackbar and its TextView as follows:
<style name="AppTheme" parent="Theme.MaterialComponents.Light.NoActionBar">
    ...
    <item name="snackbarStyle">@style/MySnackbar</item>
    <item name="snackbarTextViewStyle">@style/MySnackbarTextView</item>
</style>
  • Theme.MaterialComponents adds a margin to the snackbar, and it changes its animation. If you want the AppCompat style, do as follows:
<style name="MySnackbar" parent="@style/Widget.MaterialComponents.Snackbar">
    ...
    <item name="android:layout_margin">0dp</item>
    <item name="animationMode">slide</item>
</style>
  • Finally, we can edit our TextView:
<style name="MySnackbarTextView" parent="@style/Widget.MaterialComponents.Snackbar.TextView">
    ...
    <item name="android:gravity">center_horizontal</item>
    <item name="android:textAlignment">center</item>
</style>
查看更多
爷的心禁止访问
5楼-- · 2020-07-02 06:55

The code below does the trick for me:

TextView tv = (TextView) view.findViewById(android.support.design.R.id.snackbar_text);
if(tv!=null) {
   if (Build.VERSION.SDK_INT > Build.VERSION_CODES.JELLY_BEAN)
       tv.setTextAlignment(View.TEXT_ALIGNMENT_CENTER);

   tv.setGravity(Gravity.CENTER_HORIZONTAL);
}
查看更多
贼婆χ
6楼-- · 2020-07-02 07:02

Try this:

// make snackbar
Snackbar mSnackbar = Snackbar.make(view, R.string.intro_snackbar, Snackbar.LENGTH_LONG);
// get snackbar view
View mView = mSnackbar.getView();
// get textview inside snackbar view
TextView mTextView = (TextView) mView.findViewById(android.support.design.R.id.snackbar_text);
// set text to center
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1)
    mTextView.setTextAlignment(View.TEXT_ALIGNMENT_CENTER);
else
    mTextView.setGravity(Gravity.CENTER_HORIZONTAL);
// show the snackbar
mSnackbar.show();
查看更多
劳资没心,怎么记你
7楼-- · 2020-07-02 07:02

The above conditional setTextAlignment() OR setGravity() solution didn't work for me.

For bulletproof centered text:

  1. Always apply setGravity()
  2. Apply setTextAlignment() for API >= 17

And note the support library change in AndroidX. If using AndroidX, lookup the TextView by com.google.android.material.R.id.snackbar_text instead of android.support.design.R.id.snackbar_text.

Code:

TextView sbTextView = (TextView) findViewById(com.google.android.material.R.id.snackbar_text);

sbTextView.setGravity(Gravity.CENTER_HORIZONTAL);

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
    sbTextView.setTextAlignment(View.TEXT_ALIGNMENT_CENTER);
}
查看更多
登录 后发表回答