Android Snackbar TextAlignment in Center

前端 未结 6 1885
误落风尘
误落风尘 2021-02-18 13:30

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

Snackbar snack = Snackbar.make(findViewById(android.R.id.content), in         


        
相关标签:
6条回答
  • 2021-02-18 14:02

    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);
    }
    
    0 讨论(0)
  • 2021-02-18 14:12

    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();
    
    0 讨论(0)
  • 2021-02-18 14:16

    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);
    }
    
    0 讨论(0)
  • 2021-02-18 14:16

    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();
    
    0 讨论(0)
  • 2021-02-18 14:17

    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>
    
    0 讨论(0)
  • 2021-02-18 14:18
    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);
    }
    
    0 讨论(0)
提交回复
热议问题