繁体   English   中英

Android-自定义Toast消息的位置

[英]Android - Customizing Position of Toast Message

我有一条祝酒消息,默认设置是在屏幕底部居中显示它。 我想知道如何将其定位在TOP中心。 有任何想法吗?

谢谢

文档

您可以使用setGravity(int,int,int)方法更改此位置。 它接受三个参数:重力常数,x位置偏移和y位置偏移。

例如,如果您决定将吐司面包显示在左上角,则可以这样设置重力:

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

如果要向右微移位置,请增加第二个参数的值。 要微调它,请增加最后一个参数的值。

因此,您可以执行以下操作:

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

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

下面是我们想要的按钮

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

如果您不想使用Toast toast = Toast.makeText(context, text, duration).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();

如果您仍然SuperToast ,那么在Github有一个名为SuperToast的项目。 如果您学习它,我想您会得到很多启发。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM