setGravity修改Toast弹出的位置

时间:2022-06-01 20:06:25
                    Toast toast = Toast.makeText(SearchActivity.this, "取消关注失败",
Toast.LENGTH_SHORT);
toast.setGravity(Gravity.CENTER, 0, 0);
toast.show();

这样设置会使Toast在中间弹出



@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}

public void upperLeft(View v) {
Toast toast = Toast.makeText(this, "Upper Left!",
Toast.LENGTH_SHORT);
toast.setGravity(Gravity.TOP | Gravity.LEFT, 0, 0);
toast.show();
}

public void upperRight(View v) {
Toast toast = Toast.makeText(this, "Upper Right!",
Toast.LENGTH_SHORT);
toast.setGravity(Gravity.TOP | Gravity.RIGHT, 0, 0);
toast.show();
}

public void bottomLeft(View v) {
Toast toast = Toast.makeText(this, "Bottom Left!",
Toast.LENGTH_SHORT);

toast.setGravity(Gravity.BOTTOM | Gravity.LEFT, 0, 0);
toast.show();
}

public void bottomRight(View v) {
Toast toast = Toast.makeText(this, "Bottom Right!",
Toast.LENGTH_SHORT);
toast.setGravity(Gravity.BOTTOM | Gravity.RIGHT, 0, 0);
toast.show();
}

自己试一试吧,都是很简单能理解的东西