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

时间:2014-12-23 13:33:56

标签: java android

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

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

但是此消息显示在页面的bootom中。

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

如何制作?

6 个答案:

答案 0 :(得分:3)

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

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

答案 1 :(得分: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();

答案 2 :(得分: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);

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

官方文件来源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)方法

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

}

答案 3 :(得分:1)

在顶部显示并集中您的吐司

Toast toast = Toast.makeText(getApplicationContext(),"Network issue! Check your internet", Toast.LENGTH_LONG); 
toast.setGravity(Gravity.TOP|Gravity.CENTER, 0, 0); 
View view = toast.getView(); 
view.getBackground().setColorFilter(Color.RED, PorterDuff.Mode.SRC_IN); toast.show();

答案 4 :(得分: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();

答案 5 :(得分:0)

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

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.CENTER_VERTICAL, 0, 0);<----- toast location on screen
toast.setDuration(Toast.LENGTH_LONG);
toast.setView(layout);
toast.show();