如何设置吐司文本重力中心

时间:2017-04-17 09:59:26

标签: java android

我有一个textview,一个edittext框和一个按钮

Button YES;
TextView TV;
EditText ET;

YES.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
           TV.setText("Hello"+"\n"+ET.getText().toString());
            TV.setGravity(Gravity.CENTER);
            Toast.makeText(getApplicationContext(),TV.getText().toString(),Toast.LENGTH_SHORT).show();
        }
    });

现在,当我在文本框中输入某个名称时,它在textview中显示中心但不是Toast它不起作用。所以帮助我,我也可以将吐司文本视为中心

5 个答案:

答案 0 :(得分:1)

这是一个代码,它将在中心显示toast ui。

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

答案 1 :(得分:0)

使用Toast的setView(view)功能为View提供Gravity.CENTER

答案 2 :(得分:0)

试试这个:

YES.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
           TV.setText("Hello"+"\n"+ET.getText().toString());
           TV.setGravity(Gravity.CENTER);


           Toast toast = Toast.makeText(getApplicationContext(),TV.getText().toString(),Toast.LENGTH_SHORT);
           toast.setGravity(Gravity.CENTER, 0, 0);
           toast.show();
        }
    });

答案 3 :(得分:0)

您可以使用以下代码自定义吐司:

db.getCollection('invoice').aggregate([
    {
        $match: {
            dueDate: {$gte: 1483596800},
            dueDate: {$lte: 1583596800}
        }
    },
    {
        $lookup: {
            from: "booking",
            localField: "bookingId",
            foreignField: "_id",    
            as: "booking"       
        }   
    },
     {
        $match: {
            "booking.status": {$lt: 5},

        }
    }
])

答案 4 :(得分:0)

尝试

Toast toast = Toast.makeText(getApplicationContext(),"Hello"+"\n"+ET.getText().toString(),Toast.LENGTH_SHORT);
toast.setGravity(Gravity.CENTER, 0, 0);
toast.show();