100

Androidで画面下部にトーストメッセージを表示したいので、これを試しました:

Toast.makeText(test.this, "bbb", Toast.LENGTH_LONG).show();

うまくいきません。どうすれば正しく実行できますか?

4

12 に答える 12

290

トーストを画面中央に表示します。

Toast toast = Toast.makeText(test.this, "bbb", Toast.LENGTH_LONG);
toast.setGravity(Gravity.CENTER, 0, 0);
toast.show();
于 2013-03-10T10:25:27.520 に答える
5

Positioning your Toast

A standard toast notification appears near the bottom of the screen, centered horizontally. You can change this position with the setGravity(int, int, int) method. This accepts three parameters: a Gravity constant, an x-position offset, and a y-position offset.

For example, if you decide that the toast should appear in the top-left corner, you can set the gravity like this:

toast.setGravity(Gravity.TOP|Gravity.LEFT, 0, 0);

If you want to nudge the position to the right, increase the value of the second parameter. To nudge it down, increase the value of the last parameter.

于 2016-10-14T06:18:40.267 に答える
0
Toast.makeText(test.this, "bbb", Toast.LENGTH_LONG).apply{
setGravity(Gravity.CENTER, 0, 0)}.show()

このように、1行で実行できます。

于 2021-05-17T20:43:23.653 に答える