Android Snackbar TextAlignment in Center

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

提交回复
热议问题