我对android开发很新,但知道一点Java。我在知道在我的代码中放置Toast.setGravity(Gravity.TOP,0,0)的位置时遇到了问题。
以下是代码:
package ca.ignitestudio.geoquiz;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class QuizActivity extends AppCompatActivity {
private Button mTrueButton;
private Button mFalseButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_quiz);
mTrueButton = (Button) findViewById(R.id.true_button);
mTrueButton.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
Toast.makeText(QuizActivity.this, R.string.correct_toast, Toast.LENGTH_SHORT).show();
Toast.setGravity(Gravity.TOP,0,0);
}
});
mFalseButton = (Button) findViewById(R.id.false_button);
mFalseButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(QuizActivity.this, R.string.incorrect_toast, Toast.LENGTH_SHORT).show();
}
});
}
}
答案 0 :(得分:5)
您必须以这种方式调用setGravity:
Toast toast = Toast.makeText(test.this,"bbb", Toast.LENGTH_LONG);
toast.setGravity(Gravity.CENTER, 0, 0);
toast.show();
答案 1 :(得分:3)
setGravity
不是静态方法,因此您应该在吐司实例上调用setGravity
,即
Toast t = Toast.makeText(QuizActivity.this, R.string.correct_toast, Toast.LENGTH_SHORT);
t.setGravity(Gravity.TOP,0,0);
t.show();
答案 2 :(得分:2)
您需要在实例上调用setGravity()
,而不是在课程上调用Toast.makeText(QuizActivity.this, R.string.correct_toast, Toast.LENGTH_SHORT).show();
Toast.setGravity(Gravity.TOP,0,0);
。变化
Toast toast = Toast.makeText(QuizActivity.this, R.string.correct_toast, Toast.LENGTH_SHORT);
toast.setGravity(Gravity.TOP,0,0);
toast.show();
到
.csproj