Android - 自定义 Toast 消息的位置

Android - Customizing Position of Toast Message

我有一条吐司消息,默认设置是在屏幕底部居中显示它。我想知道如何将它定位在顶部中心。有什么想法吗?

谢谢

来自docs

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.

所以在你的情况下,你可以这样做:

//create toast object
Toast myToast = Toast.makeText(getApplicationContext(), greetings[rndy.nextInt(6)], Toast.LENGTH_SHORT);
//set gravity
myToast.setGravity(Gravity.CENTER_HORIZONTAL); //<-- set gravity here
//and show it
myToast.show();

如果您不想使用最简单的方式Toast toast = Toast.makeText(context, text, duration).show(),您可以自定义您的Toast。这是我的代码,你可以试试这个:

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

如果您还不满意,Github 中有一个名为 SuperToast 的项目。如果你研究一下,我想你会受到很多启发。

我不久前为我的一个项目实现了这个。这会将吐司放在您想要的任何视图的正下方。这种方法通常用于覆盖按钮的长按以给出按钮功能的简短描述

下面是我们要

的按钮
private View.OnLongClickListener mShareFriendsOnLongClickListener = new View.OnLongClickListener() {
    @Override
    public boolean onLongClick(View v) {
        int offsetY = 10;//getResources().getDimensionPixelSize(R.dimen.toast_offset_y);

        Toast toast = Toast.makeText(mContext, R.string.share_with_friends, Toast.LENGTH_SHORT);
        ScrapbookUtils.positionToast(toast, v, getWindow(), 0, offsetY);
        toast.show();
        return true;
    }
};

然后是完成工作的实际方法。这个实用程序可以让你把吐司放在屏幕上任何你想放的地方。

public static void positionToast(Toast toast, View view, Window window, int offsetX, int offsetY) {
    // toasts are positioned relatively to decor view, views relatively to their parents, we have to gather additional data to have a common coordinate system
    Rect rect = new Rect();
    window.getDecorView().getWindowVisibleDisplayFrame(rect);
    // covert anchor view absolute position to a position which is relative to decor view
    int[] viewLocation = new int[2];
    view.getLocationInWindow(viewLocation);
    int viewLeft = viewLocation[0] - rect.left;
    int viewTop = viewLocation[1] - rect.top;

    // measure toast to center it relatively to the anchor view
    DisplayMetrics metrics = new DisplayMetrics();
    window.getWindowManager().getDefaultDisplay().getMetrics(metrics);
    int widthMeasureSpec = MeasureSpec.makeMeasureSpec(metrics.widthPixels, MeasureSpec.UNSPECIFIED);
    int heightMeasureSpec = MeasureSpec.makeMeasureSpec(metrics.heightPixels, MeasureSpec.UNSPECIFIED);
    toast.getView().measure(widthMeasureSpec, heightMeasureSpec);
    int toastWidth = toast.getView().getMeasuredWidth();

    // compute toast offsets
    int toastX = viewLeft + (view.getWidth() - toastWidth) / 2 + offsetX;
    int toastY = viewTop + view.getHeight() + offsetY;

    toast.setGravity(Gravity.LEFT | Gravity.TOP, toastX, toastY);
}