如何在我的Android应用程序的页面顶部显示Toast消息

问题描述 投票:1回答:5

我使用以下行在我的Android应用程序中显示Toast消息

Toast.makeText(InitActivity.this, InitActivity.this.getResources().getString(R.string.invalid_pin_code), Toast.LENGTH_SHORT).show();

但是此消息显示在页面底部。

我想在我的页面中将它显示在EditText下面。

怎么做到的?

java android
5个回答
3
投票

您需要在代码中以编程方式添加此项:

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

3
投票

标准吐司通知出现在屏幕底部附近,水平居中。您可以使用setGravity(int,int,int)方法更改此位置。例如:

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

为中心

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

1
投票

试试这个 :

 LayoutInflater inflater = getLayoutInflater();
View layout = inflater.inflate(R.layout.custom_toast,
                               (ViewGroup) findViewById(R.id.toast_layout_root));

TextView text = (TextView) layout.findViewById(R.id.text);
text.setText("This is a custom toast");

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

标准吐司通知出现在屏幕底部附近,水平居中。您可以使用setGravity(int, int, int)方法更改此位置。这接受三个参数:重力常数,x位置偏移和y位置偏移。

例如,如果您确定吐司应该出现在左上角,您可以像这样设置重力:

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

如果要将位置微移到右侧,请增加第二个参数的值。要轻推它,请增加最后一个参数的值。

官方文件来源qazxsw poi

对于自定义Here检查Toast

要制作自定义位置,请关注here

编辑:为了在特定的this上做位置试试这个:

这个代码在你的View方法中

onCreate()

并添加此 editText1 = (EditText)rootView.findViewById(R.id.editText1); button1 = (Button)rootView.findViewById(R.id.button1); button1.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub makeToast("Hello", editText1); } }); 方法

makeToast(String,View)

0
投票

你应该设置吐司的重力。用这个:

public void makeToast(String text, View v) {

    Toast.makeText(getActivity(), text, Toast.LENGTH_SHORT).show();

    int xOffset = 0;
    int yOffset = 0;
    Rect gvr = new Rect();

    View parent = v;// // v is the editText,
    // parent is the rectangle holding it.

    if (parent.getGlobalVisibleRect(gvr)) {

        View root = v.getRootView();

        int halfwayWidth = root.getRight() / 2;
        int halfwayHeight = root.getBottom() / 2;
        // get the horizontal center
        int parentCenterX = ((gvr.right - gvr.left) / 2) + gvr.left;
        // get the vertical center
        int parentCenterY = (gvr.bottom - gvr.top) / 2 + gvr.top;

        if (parentCenterY <= halfwayHeight) {
            yOffset = -(halfwayHeight - parentCenterY);// this image is
                                                        // above the center
                                                        // of gravity, i.e.
                                                        // the halfwayHeight
        } else {
            yOffset = parentCenterY - halfwayHeight;
        }
        if (parentCenterX < halfwayWidth) { // this view is left of center
                                            // xOffset = -(halfwayWidth -
                                            // parentCenterX); } if
                                            // (parentCenterX >=
                                            // halfwayWidth) {
            // this view is right of center
            xOffset = parentCenterX - halfwayWidth;
        }
    }
    Toast toast = Toast.makeText(getActivity(), text, Toast.LENGTH_SHORT);
    toast.setGravity(Gravity.CENTER, xOffset, yOffset);
    toast.show();

}

0
投票

您可以使用以下方式创建自己的自定义吐司:

Toast toast = Toast.makeText(InitActivity.this, 
              InitActivity.this.getResources().getString(R.string.invalid_pin_code), 
              Toast.LENGTH_SHORT);
toast.setGravity(Gravity.TOP, 0, 0);
toast.show();
© www.soinside.com 2019 - 2024. All rights reserved.