Center text in a toast in Android

后端 未结 11 1401
没有蜡笔的小新
没有蜡笔的小新 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:06

    Toast is built on a TextView and the default gravity of it is left aligned. So, you need to create your own TextView like this for instance :

    
    

    And you assign the TextView to the Toast like this :

    Toast t = new Toast(yourContext);
    t.setView(yourNewTextView);
    

提交回复
热议问题