简体   繁体   English

Android Toast消息显示

[英]Android Toast message display

I have list of product. 我有产品清单。 When the user click on the row, the Toast msg will be displayed the name of the product. 当用户单击该行时, Toast消息将显示产品的名称。

I want to display the Toast on the each product row line.I want to show the Toast in the place where user click on list' row 我想在每个产品行上显示Toast 。我想在用户点击列表'行的位置显示Toast

See my picture; 看我的照片;

在此输入图像描述 Currently displaying center of List: 目前显示列表中心:

I did like this : 我喜欢这样:

tablerow.setOnClickListener(new View.OnClickListener() {

    public void onClick(View view) {

        Toast prName = Toast.makeText(RetailerOrderActivity.this,
            " Name : " + productList.get(Integer.parseInt(view.getTag().toString())).getDescription(),
            Toast.LENGTH_SHORT);
        prName.setGravity(Gravity.AXIS_SPECIFIED, 0, 0);
        prName.show();
    }
});

Try this, 试试这个,

int[] values= new int[2];
view.getLocationOnScreen(values);
int x = values[0];
int y = values[1] ;

toast.setGravity(Gravity.TOP|Gravity.LEFT, x, y);

The 2nd and 3rd params of setGravity() are x and y offsets from the constant specified in the 1st param. setGravity()的第2和第3个参数是第1个参数中指定的常量的x和y偏移量。 You will need to get the x&y position of the row view object you wish to position the toast above. 您需要获取要在上面放置吐司的行视图对象的x和y位置。 Or you could get the x&y of the touch event. 或者你可以得到触摸事件的x和y。

Personally I would start with setGravity(Gravity.TOP|Gravity.LEFT,0,0) 我个人会从setGravity(Gravity.TOP|Gravity.LEFT,0,0)开始setGravity(Gravity.TOP|Gravity.LEFT,0,0)

I guess you have an onClick or onItemClick -listener somewhere. 我想你有一个onClickonItemClick -listener。 If so, a View -parameter is passed to it. 如果是这样,则会向其传递View -parameter。 The passed View is the view that was clicked (the entry of your list). 传递的View是被点击的视图(列表的条目)。 You can get the Y-position of it by using the getY() -method. 您可以使用getY()方法获取它的Y位置。

You should always center the Toast horizontally and let the Toast decide it's width. 你应该总是水平居中Toast,让Toast决定它的宽度。

So your code might look something like this (using an onItemClick -listener): 所以你的代码可能看起来像这样(使用onItemClick -listener):

public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
    // Get the Y-position:
    int y = (int) view.getY();
    // Create the Toast:
    Toast toast = Toast.makeText(RetailerOrderActivity.this,
        " Name : " + productList.get(Integer.parseInt(view.getTag().toString())).getDescription(),
        Toast.LENGTH_SHORT
    );
    // Set the position:
    toast.setGravity(Gravity.CENTER_HORIZONTAL, 0, y);
    // Show the Toast:
    toast.show();
}

I did not test it, but it should work. 我没有测试它,但它应该工作。

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

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