Toast消息提示的三种方法
package com.example.test8;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;
public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void btn1Onclick(View view){
//show()方法显示
Toast.makeText(this, "Button1被点击", Toast.LENGTH_LONG).show();
}
public void btn2Onclick(View view){
//1.用构造来构建Toast
Toast toast=new Toast(this);
//2.创建要显示的view
ImageView image=new ImageView(this);
image.setImageResource(R.drawable.ic_launcher);
//3.设置属性
toast.setView(image);
toast.setDuration(1000);
toast.show();
}
public void btn3Onclick(View view){
//1.创建一个view
View toastView=LayoutInflater.from(this).inflate(R.layout.toast_view, null);
//2.用构造来构建Toast
Toast toast=new Toast(this);
//3.设置属性
toast.setView(toastView);
toast.setDuration(Toast.LENGTH_LONG);
toast.setGravity(Gravity.CENTER_HORIZONTAL, 0, 0);
toast.show();
}
}