Android中修改toast的显示位置



在xml中设置onclick属性值,内部写入一个String数据,在class文件中使用此String作为方法名,即可实现其跳转

public class ToastActivity extends BaseActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_toast);
    }

    public void onClickDefaultToast(View v) {
        Toast.makeText(this, "默认位置", Toast.LENGTH_LONG).show();
    }

    public void onClickCenterToast(View v) {
        Toast toast = Toast.makeText(this, "居中显示", Toast.LENGTH_LONG);
        toast.setGravity(Gravity.CENTER, 0, 0);
        toast.show();
    }

    public void onClickTopToast(View v) {
        Display display = getWindowManager().getDefaultDisplay();
        int height = display.getHeight();
        Toast toast = Toast.makeText(this, "上部", Toast.LENGTH_LONG);
        toast.setGravity(Gravity.TOP, 0, height / 6);
        toast.show();
    }
}
在xml中设置onclick属性值,内部写入一个String数据,在class文件中使用此String作为方法名,即可实现其跳转

public class ToastActivity extends BaseActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_toast);
    }

    public void onClickDefaultToast(View v) {
        Toast.makeText(this, "默认位置", Toast.LENGTH_LONG).show();
    }

    public void onClickCenterToast(View v) {
        Toast toast = Toast.makeText(this, "居中显示", Toast.LENGTH_LONG);
        toast.setGravity(Gravity.CENTER, 0, 0);
        toast.show();
    }

    public void onClickTopToast(View v) {
        Display display = getWindowManager().getDefaultDisplay();
        int height = display.getHeight();
        Toast toast = Toast.makeText(this, "上部", Toast.LENGTH_LONG);
        toast.setGravity(Gravity.TOP, 0, height / 6);
        toast.show();
    }
}

你可能感兴趣的:(Android中修改toast的显示位置)