控件 -- Toast

使用

1.简单使用

private void testToast() {
    Toast.makeText(this, "This is a toast", Toast.LENGTH_SHORT).show();
}

2.自定义Toast

private void testToast() {
    LinearLayout linearLayout = new LinearLayout(this);
    linearLayout.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
    linearLayout.setOrientation(LinearLayout.VERTICAL);
    ImageView imageView= new ImageView(this);
    imageView.setImageResource(R.mipmap.ic_launcher);
    TextView textView = new TextView(this);
    textView.setText("自定义Toast");
    linearLayout.addView(imageView);
    linearLayout.addView(textView);

    Toast toast= new Toast(getApplicationContext());
    toast.setGravity(Gravity.CENTER , 0, 0);
    toast.setDuration(Toast.LENGTH_LONG);
    toast.setView(linearLayout);
    toast.show();
}

你可能感兴趣的:(控件 -- Toast)