Android 自定义Toast布局

2021-01-04  本文已影响0人  因为我的心

一、前言:

1、自定义布局

有时候,我们需要自定义Toast布局样式,我们可以通过下面的方法引入一种布局,这样就能展示出来完美的效果;

LayoutInflater inflater=LayoutInflater.from(this);
View toast_view = inflater.inflate(R.layout.toast_layout,null);
Toast toast=new Toast(this);
toast.setView(toast_view);
toast.show();

二、显示弹窗的位置

1、中间位置弹窗

Toast toast = Toast.makeText(SearchActivity.this, "取消关注失败",
         Toast.LENGTH_SHORT);
   toast.setGravity(Gravity.CENTER, 0, 0);
   toast.show();

2、上方左侧位置弹窗

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();
  }

3、上方右侧位置弹窗

  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();
  }

4、左侧下方位置弹窗

  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();
  }

5、右侧下方位置弹窗

  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();
  }
上一篇下一篇

猜你喜欢

热点阅读