今天学自定义Toast。好吧,原谅我周末偷懒了!先弄2个Button。
?
- <RelativeLayout?xmlns:android="http://schemas.android.com/apk/res/android"??
- ????xmlns:tools="http://schemas.android.com/tools"??
- ????android:layout_width="match_parent"??
- ????android:layout_height="match_parent"?>??
- ??
- ????<Button??
- ????????android:id="@+id/toast_btn"??
- ????????android:layout_width="wrap_content"??
- ????????android:layout_height="wrap_content"??
- ????????android:layout_alignParentTop="true"??
- ????????android:layout_centerHorizontal="true"??
- ????????android:layout_marginTop="17dp"??
- ????????android:text="吐司"??
- ????????android:textSize="30sp"?/>??
- ??
- ????<Button??
- ????????android:id="@+id/notice_btn"??
- ????????android:layout_width="wrap_content"??
- ????????android:layout_height="wrap_content"??
- ????????android:layout_alignLeft="@+id/toast_btn"??
- ????????android:layout_below="@+id/toast_btn"??
- ????????android:layout_marginTop="46dp"??
- ????????android:text="通知"?/>??
- ??
- </RelativeLayout>??
在activity里面找到这2个Button ,添加监听
?
?
- toastBtn=(Button)?findViewById(R.id.toast_btn);??
- noticeBtn=(Button)?findViewById(R.id.notice_btn);??
- //给按钮添加监听??
- toastBtn.setOnClickListener(this);??
- noticeBtn.setOnClickListener(this);??
判断单击了哪个View
- public?void?onClick(View?v)?{??
- ????????//判断单击了哪个View??
- ????????switch?(v.getId())?{??
- ????????case?R.id.toast_btn:??
- ????????????//显示Toast??
- ????????????toast(v);?????
- ????????????break;??
- ????????case?R.id.notice_btn:??
- ????????????notice(v);??
- ????????}?????
- ????}??
?
- private?void?notice(View?v)?{??
- ????????NotificationManager?mgr=(NotificationManager)?getSystemService(Context.NOTIFICATION_SERVICE);??
- ????????Notification?notification=new?Notification(R.drawable.sym_call_missed,???
- ????????????????"你有一个未接电话",?System.currentTimeMillis());??
- ????????//意图??
- ????????Intent?intent=new?Intent(context,DetailActivity.class);??
- ????????//条件触发意图,转移activity??
- ????????PendingIntent?pi=PendingIntent.getActivity(context,?0,?intent,?0);??
- ????????notification.setLatestEventInfo(context,?"未接电话",?"查看来源",?pi);??
- ????????//默认提示的声音??
- ????????notification.defaults=Notification.DEFAULT_SOUND;??
- ????????//发出通知??
- ????????mgr.notify(1,?notification);??
- ??
- ??????????
- ????}??
?
- private?void?toast(View?v)?{??
- ????????Toast?toast=new?Toast(context);??
- ????????//设置view(就是Toast显示的界面)??
- ????????//构建一个线性布局??
- ????????LinearLayout?layout=new?LinearLayout(context);??
- ????????layout.setBackgroundResource(R.drawable.bg_yellow);??
- ????????layout.setGravity(Gravity.CENTER);??
- ????????//设置此布局为水平线性布局??
- ????????layout.setOrientation(LinearLayout.HORIZONTAL);??
- ????????//构建一个图片??
- ????????ImageView?image=new?ImageView(context);??
- ????????image.setBackgroundResource(R.drawable.ic_toast);??
- ????????//添加图片到布局??
- ????????layout.addView(image);??
- ????????//构建一个文本资源??
- ????????TextView?text=new?TextView(context);??
- ????????text.setText("冰激凌");??
- ????????layout.addView(text);??
- ????????toast.setView(layout);??
- ????????//设置子控件的位置??
- ????????toast.setGravity(Gravity.CENTER,?0,?0);//偏移量??
- ????????//设置Toast的显示时间??
- ????????toast.setDuration(Toast.LENGTH_LONG);??
- ????????//显示吐司??
- ????????toast.show();?????
- ????}??
?
好吧,就这样吧。。