
Android中Toast的几种使用方法
2个回答
展开全部
Toast是Android中用来显示显示信息的一种机制,和Dialog不一样的是,Toast是没有焦点的,而且Toast显示的时间有限,过一定的时间就会自动消失。有几种使用方式:
1、默认效果 代码
Toast.makeText(getApplicationContext(),"默认Toast样式",Toast.LENGTH_SHORT).show();
2、自定义显示位置效果 代码
toast= Toast.makeText(getApplicationContext(),
"自定义位置Toast",Toast.LENGTH_LONG);
toast.setGravity(Gravity.CENTER, 0, 0);
toast.show();
3、带图片效果 代码
toast= Toast.makeText(getApplicationContext(),
"带图片的Toast",Toast.LENGTH_LONG);
toast.setGravity(Gravity.CENTER, 0, 0);
LinearLayout toastView = (LinearLayout)toast.getView();
ImageView imageCodeProject = newImageView(getApplicationContext());
imageCodeProject.setImageResource(R.drawable.icon);
toastView.addView(imageCodeProject, 0);
toast.show();
4、完全自定义效果
代码
LayoutInflaterinflater = getLayoutInflater();
View layout =inflater.inflate(R.layout.custom,
(ViewGroup)findViewById(R.id.llToast));
ImageView image = (ImageView) layout
.findViewById(R.id.tvImageToast);
image.setImageResource(R.drawable.icon);
TextView title = (TextView)layout.findViewById(R.id.tvTitleToast);
title.setText("Attention");
TextView text = (TextView) layout.findViewById(R.id.tvTextToast);
text.setText("完全自定义Toast");
toast = new Toast(getApplicationContext());
toast.setGravity(Gravity.RIGHT | Gravity.TOP,12, 40);
toast.setDuration(Toast.LENGTH_LONG);
toast.setView(layout);
toast.show();
5.其他线程
代码
newThread(new Runnable() {
public void run() {
showToast();
}
}).start();
1、默认效果 代码
Toast.makeText(getApplicationContext(),"默认Toast样式",Toast.LENGTH_SHORT).show();
2、自定义显示位置效果 代码
toast= Toast.makeText(getApplicationContext(),
"自定义位置Toast",Toast.LENGTH_LONG);
toast.setGravity(Gravity.CENTER, 0, 0);
toast.show();
3、带图片效果 代码
toast= Toast.makeText(getApplicationContext(),
"带图片的Toast",Toast.LENGTH_LONG);
toast.setGravity(Gravity.CENTER, 0, 0);
LinearLayout toastView = (LinearLayout)toast.getView();
ImageView imageCodeProject = newImageView(getApplicationContext());
imageCodeProject.setImageResource(R.drawable.icon);
toastView.addView(imageCodeProject, 0);
toast.show();
4、完全自定义效果
代码
LayoutInflaterinflater = getLayoutInflater();
View layout =inflater.inflate(R.layout.custom,
(ViewGroup)findViewById(R.id.llToast));
ImageView image = (ImageView) layout
.findViewById(R.id.tvImageToast);
image.setImageResource(R.drawable.icon);
TextView title = (TextView)layout.findViewById(R.id.tvTitleToast);
title.setText("Attention");
TextView text = (TextView) layout.findViewById(R.id.tvTextToast);
text.setText("完全自定义Toast");
toast = new Toast(getApplicationContext());
toast.setGravity(Gravity.RIGHT | Gravity.TOP,12, 40);
toast.setDuration(Toast.LENGTH_LONG);
toast.setView(layout);
toast.show();
5.其他线程
代码
newThread(new Runnable() {
public void run() {
showToast();
}
}).start();
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
我现在的设置是居中靠顶�0�2�0�2�0�2�0�2�0�2�0�2�0�2�0�2 //第二个参数:相对于第一个参数设置toast位置的横向X轴的偏移量,正数向右偏移,负数向左偏移�0�2�0�2�0�2�0�2�0�2�0�2�0�2�0�2 //第三个参数:同的第二个参数道理一样�0�2�0�2�0�2�0�2�0�2�0�2�0�2�0�2 //如果你设置的偏移量超过了屏幕的范围,toast将在屏幕内靠近超出的那个边界显示�0�2�0�2�0�2�0�2�0�2�0�2�0�2�0�2 toast.setGravity(Gravity.TOP|Gravity.CENTER, -50, 100);�0�2�0�2�0�2�0�2�0�2�0�2�0�2�0�2�0�2 //屏幕居中显示,X轴和Y轴偏移量都是0�0�2�0�2�0�2�0�2�0�2�0�2�0�2�0�2 //toast.setGravity(Gravity.CENTER, 0, 0); �0�2�0�2�0�2�0�2�0�2�0�2�0�2�0�2 toast.show();�0�2�0�23.带图片的Java代码Toast toast=Toast.makeText(getApplicationContext(), “显示带图片的toast”, 3000);�0�2�0�2�0�2�0�2�0�2�0�2�0�2�0�2 toast.setGravity(Gravity.CENTER, 0, 0);�0�2�0�2�0�2�0�2�0�2�0�2�0�2�0�2�0�2 //创建图片视图对象�0�2�0�2�0�2�0�2�0�2�0�2�0�2�0�2 ImageView imageView= new ImageView(getApplicationContext());�0�2�0�2�0�2�0�2�0�2�0�2�0�2�0�2 //设置图片�0�2�0�2�0�2�0�2�0�2�0�2�0�2�0�2 imageView.setImageResource(R.drawable.ic_launcher);�0�2�0�2�0�2�0�2�0�2�0�2�0�2�0�2 //获得toast的布局�0�2�0�2�0�2�0�2�0�2�0�2�0�2�0�2 LinearLayout toastView = (LinearLayout) toast.getView();�0�2�0�2�0�2�0�2�0�2�0�2�0�2�0�2 //设置此布局为横向的�0�2�0�2�0�2�0�2�0�2�0�2�0�2�0�2 toastView.setOrientation(LinearLayout.HORIZONTAL);�0�2�0�2�0�2�0�2�0�2�0�2�0�2�0�2 //将ImageView在加入到此布局中的第一个位置�0�2�0�2�0�2�0�2�0�2�0�2�0�2�0�2 toastView.addView(imageView, 0);�0�2�0�2�0�2�0�2�0�2�0�2�0�2�0�2 toast.show();�0�2�0�24.完全自定义显示方式Java代码//Inflater意思是充气�0�2�0�2�0�2�0�2�0�2�0�2�0�2 //LayoutInflater这个类用来实例化XML文件到其相应的视图对象的布局�0�2�0�2�0�2�0�2�0�2�0�2�0�2 LayoutInflater inflater = getLayoutInflater();�0�2�0�2�0�2�0�2�0�2�0�2�0�2 //通过制定XML文件及布局ID来填充一个视图对象�0�2�0�2�0�2�0�2�0�2�0�2�0�2 View layout = inflater.inflate(R.layout.custom2,(ViewGroup)findViewById(R.id.llToast));�0�2�0�2�0�2�0�2�0�2�0�2�0�2�0�2�0�2�0�2�0�2�0�2�0�2�0�2�0�2 ImageView image = (ImageView) layout.findViewById(R.id.tvImageToast);�0�2�0�2�0�2�0�2�0�2�0�2�0�2 //设置布局中图片视图中图片�0�2�0�2�0�2�0�2�0�2�0�2�0�2 image.setImageResource(R.drawable.ic_launcher);�0�2�0�2�0�2�0�2�0�2�0�2�0�2�0�2�0�2�0�2�0�2�0�2�0�2�0�2�0�2 TextView title = (TextView) layout.findViewById(R.id.tvTitleToast);�0�2�0�2�0�2�0�2�0�2�0�2�0�2 //设置标题�0�2�0�2�0�2�0�2�0�2�0�2�0�2 title.setText(“标题栏”);�0�2�0�2�0�2�0�2�0�2�0�2�0�2�0�2�0�2�0�2�0�2�0�2�0�2�0�2�0�2 TextView text = (TextView) layout.findViewById(R.id.tvTextToast);�0�2�0�2�0�2�0�2�0�2�0�2�0�2 //设置内容�0�2�0�2�0�2�0�2�0�2�0�2�0�2 text.setText(“完全自定义Toast”);�0�2�0�2�0�2�0�2�0�2�0�2�0�2�0�2�0�2�0�2�0�2�0�2�0�2�0�2�0�2 Toast toast= new Toast(getApplicationContext());�0�2�0�2�0�2�0�2�0�2�0�2�0�2 toast.setGravity(Gravity.CENTER , 0, 0);�0�2�0�2�0�2�0�2�0�2�0�2�0�2 toast.setDuration(Toast.LENGTH_LONG);�0�2�0�2�0�2�0�2�0�2�0�2�0�2 toast.setView(layout);�0�2�0�2�0�2�0�2�0�2�0�2�0�2 toast.show();�0�2�0�25.其他线程通过Handler的调用Java代码//调用方法1�0�2�0�2�0�2�0�2 //Thread th=new Thread(this);�0�2�0�2�0�2�0�2 //th.start();�0�2�0�2�0�2�0�2 //调用方法2�0�2�0�2�0�2�0�2 handler.post(new Runnable() {�0�2�0�2�0�2�0�2�0�2�0�2�0�2�0�2 @Override�0�2�0�2�0�2�0�2�0�2�0�2�0�2�0�2 public void run() {�0�2�0�2�0�2�0�2�0�2�0�2�0�2�0�2�0�2�0�2�0�2�0�2 showToast();�0�2�0�2�0�2�0�2�0�2�0�2�0�2�0�2 }�0�2�0�2�0�2�0�2 });�0�2�0�2Java代码public void showToast(){�0�2�0�2�0�2�0�2�0�2�0�2�0�2�0�2 Toast toast=Toast.makeText(getApplicationContext(), “Toast在其他线程中调用显示”, Toast.LENGTH_SHORT);�0�2�0�2�0�2�0�2�0�2�0�2�0�2�0�2 toast.show();�0�2�0�2�0�2�0�2 }�0�2�0�2Java代码Handler handler=new Handler(){�0�2�0�2�0�2�0�2�0�2�0�2�0�2�0�2 @Override�0�2�0�2�0�2�0�2�0�2�0�2�0�2�0�2 public void handleMessage(Message msg) {�0�2�0�2�0�2�0�2�0�2�0�2�0�2�0�2�0�2�0�2�0�2�0�2 int what=msg.what;�0�2�0�2�0�2�0�2�0�2�0�2�0�2�0�2�0�2�0�2�0�2�0�2 switch (what) {�0�2�0�2�0�2�0�2�0�2�0�2�0�2�0�2�0�2�0�2�0�2�0�2 case 1:�0�2�0�2�0�2�0�2�0�2�0�2�0�2�0�2�0�2�0�2�0�2�0�2�0�2�0�2�0�2�0�2 showToast();�0�2�0�2�0�2�0�2�0�2�0�2�0�2�0�2�0�2�0�2�0�2�0�2�0�2�0�2�0�2�0�2 break;�0�2�0�2�0�2�0�2�0�2�0�2�0�2�0�2�0�2�0�2�0�2�0�2 default:�0�2�0�2�0�2�0�2�0�2�0�2�0�2�0�2�0�2�0�2�0�2�0�2�0�2�0�2�0�2�0�2 break;�0�2�0�2�0�2�0�2�0�2�0�2�0�2�0�2�0�2�0�2�0�2�0�2 }�0�2�0�2�0�2�0�2�0�2�0�2�0�2�0�2�0�2�0�2�0�2�0�2�0�2�0�2�0�2�0�2�0�2�0�2�0�2�0�2�0�2�0�2�0�2�0�2�0�2 super.handleMessage(msg);�0�2�0�2�0�2�0�2�0�2�0�2�0�2�0�2 }�0�2�0�2�0�2�0�2 };�0�2�0�2Java代码@Override�0�2�0�2�0�2�0�2 public void run() {�0�2�0�2�0�2�0�2�0�2�0�2�0�2�0�2 handler.sendEmptyMessage(1);�0�2�0�2�0�2�0�2 }�0�2
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询