วิธีแสดง Toast ตรงกลางหน้าจอ


89

In Android I want to display a toast message at the bottom of the screen, I tried this:

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

It doesn't work, how do I do it correctly?


It didn't make any sense to me. What are you exactly trying to say?
SudoRahul

1
format text, correct question, sample code what did you try, did you even try use search engine ?
deadfish

People who edit this question edit it wrong... The question is clear, the message body is not... please edit it again. What he want to ask is "How to display Toast message at center of the screen" , the message is "In android I want to display a Toast message at the center of the screen, to display a toast message at the bottom of the screen I try this Toast.makeText(test.this,"bbb", Toast.LENGTH_LONG).show(); How do I place a Toast message at the middle of the screen?" I think was something like that, not as it was edited...
Elsanty

I love this article. That's why I am sharing it. Android Toast Example - click here
Athira Reddy

คำตอบ:


271

To display the Toast in center of the screen.

Toast toast = Toast.makeText(test.this,"bbb", Toast.LENGTH_LONG);
toast.setGravity(Gravity.CENTER, 0, 0);
toast.show();

If someone wants to adjust the position further, the third argument in setGravity takes in the yAxis offset in pixels.
Sagar

4

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.


2

ใน Xamarin Android สิ่งนี้จะแสดงขนมปังที่ตรงกลางหน้าจอ:

            Toast toast = Toast.MakeText(ApplicationContext, "bbb", ToastLength.Long);
            toast.SetGravity(GravityFlags.Center, 0, 0);
            toast.Show();

2

ไฟล์เค้าโครงสำหรับขนมปังปิ้งแบบกำหนดเอง

<ImageView
android:id="@+id/image"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_marginRight="5dp" />

<TextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:textColor="#000" />

ไฟล์. java สำหรับขนมปังปิ้งที่กำหนดเองในเหตุการณ์การคลิกของปุ่ม

public class MainActivity extends Activity {

private Button button;

public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    button = (Button) findViewById(R.id.buttonToast);

    button.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {

            // get your custom_toast.xml ayout
            LayoutInflater inflater = getLayoutInflater();

            View layout = inflater.inflate(R.layout.custom_toast,
              (ViewGroup) findViewById(R.id.custom_toast_layout_id));

            // set a dummy image
            ImageView image = (ImageView) layout.findViewById(R.id.image);
            image.setImageResource(R.drawable.ic_launcher);

            // set a message
            TextView text = (TextView) layout.findViewById(R.id.text);
            text.setText("Button is clicked!");

            // Toast...
            Toast toast = new Toast(getApplicationContext());
            toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
            toast.setDuration(Toast.LENGTH_LONG);
            toast.setView(layout);
            toast.show();
        }
    });
}

}



0

การแสดง / ตั้งค่าแรงโน้มถ่วงของข้อความที่ศูนย์กลาง (แนวนอน) ในโคลติน

fun Context.longToast(msg: String) {
    Toast.makeText(this, msg, Toast.LENGTH_LONG)
        .apply {
           view.findViewById<TextView>(android.R.id.message)?.gravity = Gravity.CENTER
        }
        .show()
}


-2

รหัสด้านล่างใช้ได้ผลสำหรับฉัน

Toast.makeText(this, "Toast in center", Toast.LENGTH_SHORT).setGravity(Gravity.CENTER,0,0).show();

setGravity ไม่ส่งคืนวัตถุ Toast โมฆะ setGravity (int แรงโน้มถ่วง, int xOffset, int yOffset)
wrkwrk
โดยการใช้ไซต์ของเรา หมายความว่าคุณได้อ่านและทำความเข้าใจนโยบายคุกกี้และนโยบายความเป็นส่วนตัวของเราแล้ว
Licensed under cc by-sa 3.0 with attribution required.