当前位置: 代码迷 >> Android >> 小弟我的android 第三天 - 自定义Toast
  详细解决方案

小弟我的android 第三天 - 自定义Toast

热度:328   发布时间:2016-04-28 07:21:40.0
我的android 第三天 - 自定义Toast

今天学自定义Toast。好吧,原谅我周末偷懒了!先弄2个Button。

?

[plain]?view plaincopy
?
  1. <RelativeLayout?xmlns:android="http://schemas.android.com/apk/res/android"??
  2. ????xmlns:tools="http://schemas.android.com/tools"??
  3. ????android:layout_width="match_parent"??
  4. ????android:layout_height="match_parent"?>??
  5. ??
  6. ????<Button??
  7. ????????android:id="@+id/toast_btn"??
  8. ????????android:layout_width="wrap_content"??
  9. ????????android:layout_height="wrap_content"??
  10. ????????android:layout_alignParentTop="true"??
  11. ????????android:layout_centerHorizontal="true"??
  12. ????????android:layout_marginTop="17dp"??
  13. ????????android:text="吐司"??
  14. ????????android:textSize="30sp"?/>??
  15. ??
  16. ????<Button??
  17. ????????android:id="@+id/notice_btn"??
  18. ????????android:layout_width="wrap_content"??
  19. ????????android:layout_height="wrap_content"??
  20. ????????android:layout_alignLeft="@+id/toast_btn"??
  21. ????????android:layout_below="@+id/toast_btn"??
  22. ????????android:layout_marginTop="46dp"??
  23. ????????android:text="通知"?/>??
  24. ??
  25. </RelativeLayout>??

在activity里面找到这2个Button ,添加监听

?

?

[java]?view plaincopy
?
  1. toastBtn=(Button)?findViewById(R.id.toast_btn);??
  2. noticeBtn=(Button)?findViewById(R.id.notice_btn);??
  3. //给按钮添加监听??
  4. toastBtn.setOnClickListener(this);??
  5. noticeBtn.setOnClickListener(this);??


判断单击了哪个View

[java]?view plaincopy
?
  1. public?void?onClick(View?v)?{??
  2. ????????//判断单击了哪个View??
  3. ????????switch?(v.getId())?{??
  4. ????????case?R.id.toast_btn:??
  5. ????????????//显示Toast??
  6. ????????????toast(v);?????
  7. ????????????break;??
  8. ????????case?R.id.notice_btn:??
  9. ????????????notice(v);??
  10. ????????}?????
  11. ????}??

?

[java]?view plaincopy
?
  1. private?void?notice(View?v)?{??
  2. ????????NotificationManager?mgr=(NotificationManager)?getSystemService(Context.NOTIFICATION_SERVICE);??
  3. ????????Notification?notification=new?Notification(R.drawable.sym_call_missed,???
  4. ????????????????"你有一个未接电话",?System.currentTimeMillis());??
  5. ????????//意图??
  6. ????????Intent?intent=new?Intent(context,DetailActivity.class);??
  7. ????????//条件触发意图,转移activity??
  8. ????????PendingIntent?pi=PendingIntent.getActivity(context,?0,?intent,?0);??
  9. ????????notification.setLatestEventInfo(context,?"未接电话",?"查看来源",?pi);??
  10. ????????//默认提示的声音??
  11. ????????notification.defaults=Notification.DEFAULT_SOUND;??
  12. ????????//发出通知??
  13. ????????mgr.notify(1,?notification);??
  14. ??
  15. ??????????
  16. ????}??

?

[java]?view plaincopy
?
  1. private?void?toast(View?v)?{??
  2. ????????Toast?toast=new?Toast(context);??
  3. ????????//设置view(就是Toast显示的界面)??
  4. ????????//构建一个线性布局??
  5. ????????LinearLayout?layout=new?LinearLayout(context);??
  6. ????????layout.setBackgroundResource(R.drawable.bg_yellow);??
  7. ????????layout.setGravity(Gravity.CENTER);??
  8. ????????//设置此布局为水平线性布局??
  9. ????????layout.setOrientation(LinearLayout.HORIZONTAL);??
  10. ????????//构建一个图片??
  11. ????????ImageView?image=new?ImageView(context);??
  12. ????????image.setBackgroundResource(R.drawable.ic_toast);??
  13. ????????//添加图片到布局??
  14. ????????layout.addView(image);??
  15. ????????//构建一个文本资源??
  16. ????????TextView?text=new?TextView(context);??
  17. ????????text.setText("冰激凌");??
  18. ????????layout.addView(text);??
  19. ????????toast.setView(layout);??
  20. ????????//设置子控件的位置??
  21. ????????toast.setGravity(Gravity.CENTER,?0,?0);//偏移量??
  22. ????????//设置Toast的显示时间??
  23. ????????toast.setDuration(Toast.LENGTH_LONG);??
  24. ????????//显示吐司??
  25. ????????toast.show();?????
  26. ????}??



?

好吧,就这样吧。。

  相关解决方案