《Android编程权威指南(第3版)》:1.11 挑战练习

这个练习需要你定制toast消息,改在屏幕顶部而不是底部显示弹出消息。这需要使用Toast 类的setGravity方法,并使用Gravity.TOP重力值。具体如何使用,请参考Android开发者文档。 该方法所在网页为 developer.android.com/reference/android/widget/Toast.html#setGravity(int, int, int)。
 

mTrueButton = (Button)findViewById(R.id.true_button);
        mTrueButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast t = Toast.makeText(QuizActivity.this, R.string.correct_toast, Toast.LENGTH_SHORT);
                t.setGravity(Gravity.TOP, 0, 100);//xOffset代表水平向右的位移,yOffset代表竖直向下的位移
                t.show();
                //Toast.makeText(QuizActivity.this, R.string.correct_toast, Toast.LENGTH_SHORT).show();
            }
        });

猜你喜欢

转载自blog.csdn.net/qq_39479426/article/details/82384215