Android Snackbar TextAlignment in Center

前端 未结 6 1879
误落风尘
误落风尘 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: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();
    

提交回复
热议问题