Android Set the text align to the middle of the toast [duplicate]

匿名 (未验证) 提交于 2019-12-03 07:36:14

问题:

This question already has an answer here:

I have a toast message which is pretty long. I would like to set the text in the middle and not to start align to the left.

Is this possible?

回答1:

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 :

<TextView        android:layout_width="fill_parent"       android:layout_height="fill_parent"       android:gravity="center_vertical|center_horizontal"      android:text="all the text you want" />  

And you assign the TextView to the Toast like this :

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

Source



回答2:

Here:

Toast toast = Toast.makeText(this, "Message", Toast.LENGTH_SHORT); toast.setGravity(Gravity.CENTER, 0, 0); toast.show(); 


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!