Android使用Toast显示消息提示框

实现Toast消息提示框有两种方法
一、通过Toast的makeText方法

 Toast.makeText(this,"makeText消息提示框",Toast.LENGTH_SHORT).show();

二、通过Toast构造方法

Toast toast=new Toast(getBaseContext());
                LinearLayout linearLayout=new LinearLayout(getBaseContext());//新建线性布局管理器
                linearLayout.setOrientation(LinearLayout.VERTICAL);//设置垂直分布
                linearLayout.setBackgroundColor(Color.RED);//设置背景颜色
                //新建ImagView控件并添加到linearLayout线性布局管理器里
                ImageView imageView=new ImageView(getBaseContext());
                imageView.setImageResource(R.mipmap.ic_launcher);
                linearLayout.addView(imageView);
                 //新建TextView控件并添加到linearLayout线性布局管理器里
                TextView textView=new TextView(getBaseContext());
                textView.setText("构造方法实现消息提示框");
                linearLayout.addView(textView);
     
                toast.setView(linearLayout);//线性布局管理器添加到Toast里
                toast.setGravity(Gravity.CENTER_HORIZONTAL,0,200);//设置Toast对齐方式及x、y方向偏移
                toast.setDuration(Toast.LENGTH_SHORT);//设置Toast持续时间
                toast.show();

你可能感兴趣的:(android)