Android Snackbar TextAlignment in Center

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

提交回复
热议问题