Toast

本文详细介绍了Android中Toast的使用方法,包括如何改变Toast的位置、如何使用自定义布局以及如何实现Toast长时间显示的效果。同时提供了Toast部分源码解析,帮助开发者更好地理解和应用Toast。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Toast的基本原理其实就是将一个View添加到WindowManager中,让WindowManager来把View显示出来。(WindowManager可以将View显示在任何地方,任何Activity之上)


Toast的默认属性
Java代码 收藏代码
  1. //对其方式为:水平居中,并在底部
  2. mGravity=Gravtiy.CENTER_HORIZONTAL|Gravtiy.BOTTOM;
  3. mX=0;
  4. mY=context.getResources().getDimensionPixelSize(com.android.internal.R.dimen.toast_y_offset);
  5. mHorizontalMargin=0;
  6. mVerticalMargin=0;
  7. 所以用Toast.makeText(getApplicationContext(),R.string.text,Toast.LENGTH_SHORT).show();生成的Toast总是处在底部水平居中的位置



在指定x, y处显示Toast
Java代码 收藏代码
  1. //在(50,100)处显示Toast
  2. Toasttoast=Toast.makeText(getApplicationContext(),"toastuse",Toast.LENGTH_SHORT);
  3. toast.setGravity(Gravity.TOP|Gravity.LEFT,50,100);
  4. toast.show();
  5. //如果使用Gravity.NO_GRAVITY,后面的x,y就是相对于屏幕的中心点的(估计android是默认这么处理的)
  6. Toasttoast=Toast.makeText(getApplicationContext(),"toastuse",Toast.LENGTH_SHORT);
  7. toast.setGravity(Gravity.NO_GRAVITY,50,100);
  8. toast.show();
  9. //用margin来控制toast的位置
  10. Toasttoast=Toast.makeText(getApplicationContext(),"toastuse",Toast.LENGTH_SHORT);
  11. toast.setGravity(Gravity.LEFT|Gravity.TOP,0,0);
  12. //leftMargin,topMargin分别是容器width,height的%多少(这里是10%和20%)
  13. toast.setMargin(0.1F,0.2F);
  14. toast.show();



指定View的Toast
Java代码 收藏代码
  1. //布局xml:R.layout.toast
  2. <Button
  3. xmlns:android="http://schemas.android.com/apk/res/android"
  4. android:id="@android:id/message"
  5. android:layout_width="fill_parent"
  6. android:layout_height="wrap_content"/>
  7. Toasttoast=newToast(getApplicationContext());
  8. toast.setView(LayoutInflater.from(getApplicationContext()).inflate(R.layout.toast,null));
  9. toast.setText("toastuse");
  10. //Button是否fill_parent是由gravity控制的,xml中的不起任何作用
  11. toast.setGravity(toast.getGravity()|Gravity.FILL_HORIZONTAL,
  12. toast.getXOffset(),toast.getYOffset());
  13. toast.setDuration(Toast.LENGTH_SHORT);
  14. toast.show();

Toast部分源码
Java代码 收藏代码
  1. //Toast的构造器只设置了mY这个属性。mNextView,mDuration都没有设置(用makeText的话,这两个属性会设置)
  2. publicToast(Contextcontext){
  3. mContext=context;
  4. mTN=newTN();
  5. mY=context.getResources().getDimensionPixelSize(com.android.internal.R.dimen.toast_y_offset);
  6. }
  7. //setText方法,需要将显示text的view的id设为@android:id/message,否则会抛RuntimeException
  8. publicvoidsetText(CharSequences){
  9. if(mNextView==null){
  10. thrownewRuntimeException("ThisToastwasnotcreatedwithToast.makeText()");
  11. }
  12. TextViewtv=(TextView)mNextView.findViewById(com.android.internal.R.id.message);
  13. if(tv==null){
  14. thrownewRuntimeException("ThisToastwasnotcreatedwithToast.makeText()");
  15. }
  16. tv.setText(s);
  17. }




一直显示的Toast
实现原理是:在Toast隐藏之前,再show一个相同的Toast,来实现长显示的假象
Java代码 收藏代码
  1. privateclassToastWrapper{
  2. privateToastmToast;
  3. privateHandlermHandler;
  4. privateRunnablemShowToast=newRunnable(){
  5. @Override
  6. publicvoidrun(){
  7. continueShow();
  8. }
  9. };
  10. privatebooleanmCancelled=true;
  11. publicToastWrapper(Contextctxt){
  12. this(ctxt,newHandler());
  13. }
  14. publicToastWrapper(Contextctxt,Handlerhandler){
  15. mToast=Toast.makeText(ctxt,null,Toast.LENGTH_SHORT);
  16. mHandler=handler;
  17. }
  18. publicToastgetToast(){
  19. returnmToast;
  20. }
  21. publicvoidshowUntilCancel(){
  22. if(mCancelled){
  23. mCancelled=false;
  24. mToast.setDuration(Toast.LENGTH_LONG);
  25. continueShow();
  26. }
  27. }
  28. publicvoidcancel(){
  29. mCancelled=true;
  30. mToast.cancel();
  31. }
  32. privatevoidcontinueShow(){
  33. if(mCancelled){
  34. return;
  35. }
  36. mToast.show();
  37. mHandler.postDelayed(mShowToast,3000);
  38. }
  39. }

使用ToastWrapper
Java代码 收藏代码
  1. //一直显示的toast
  2. toastWrapper=newToastWrapper(getApplicationContext());
  3. Toasttoast=toastWrapper.getToast();
  4. toast.setText("toastwrapper");
  5. //...
  6. Buttonbutton=newButton(getApplicationContext());
  7. button.setText("一直显示toast");
  8. button.setOnClickListener(newView.OnClickListener(){
  9. @Override
  10. publicvoidonClick(Viewview){
  11. toastWrapper.showUntilCancel();
  12. }
  13. });
  14. Buttonbutton=newButton(getApplicationContext());
  15. button.setText("隐藏toast");
  16. button.setOnClickListener(newView.OnClickListener(){
  17. @Override
  18. publicvoidonClick(Viewview){
  19. toastWrapper.cancel();
  20. }
  21. });
  22. //一搬的toast
  23. Buttonbutton=newButton(getApplicationContext());
  24. button.setText("一般的toast");
  25. button.setOnClickListener(newView.OnClickListener(){
  26. @Override
  27. publicvoidonClick(Viewview){
  28. Toasttoast=toastWrapper.getToast();
  29. toast.setDuration(Toast.LENGTH_SHORT);
  30. toast.show();
  31. }
  32. });

Android服务service里面出Toast:

  1. if(phoneIsInUse())
  2. {
  3. newThread(newRunnable(){
  4. publicvoidrun(){
  5. Looper.prepare();
  6. Toast
  7. .makeText(
  8. VuiService.this,
  9. "请结束通话后再试",Toast.LENGTH_LONG).show();
  10. Looper.loop();
  11. }
  12. }).start();
  13. return;
  14. }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值