Center text in a toast in Android

后端 未结 11 1403
没有蜡笔的小新
没有蜡笔的小新 2020-12-13 01:58

I was wondering if there was a way to display all text in a toast to be centered. For instance, I have a toast that has 2 lines of text in it. For purely aesthetic reasons,

11条回答
  •  温柔的废话
    2020-12-13 02:09

    Without the hacks:

    String text = "Some text";
    Spannable centeredText = new SpannableString(text);
    centeredText.setSpan(new AlignmentSpan.Standard(Layout.Alignment.ALIGN_CENTER),
                0, text.length() - 1,
                Spannable.SPAN_INCLUSIVE_INCLUSIVE);
    
    Toast.makeText(getActivity(), centeredText, Toast.LENGTH_LONG).show();
    

    There are also another alignments besides center.

    source

提交回复
热议问题