我的android 第三天 - 自定义Toast
来源:互联网 发布:国产碳纤维 知乎 编辑:程序博客网 时间:2024/05/22 09:41
我的android 第三天 - 自定义Toast
今天学自定义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) {//判断单击了哪个Viewswitch (v.getId()) {case R.id.toast_btn://显示Toasttoast(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);//条件触发意图,转移activityPendingIntent 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();}
效果演示:
点击第一个button
点击第二个button
点击这条信息跳转到DetailActivity
大概就这样吧。。。
- 我的android 第三天 - 自定义Toast
- 我的android(第三天)
- Android:自定义的Toast
- 我的第三天学习
- Android自定义Toast取代系统的Toast
- android自定义风格的toast
- android自定义的toast样式
- Android自定义Toast的使用
- Android自定义弹出的Toast
- 我的第三天:错误总结
- 自定义Toast 吐出我的风格
- Android 更改 Toast 的默认位置及自定义Toast
- Android中Toast的自定义的样式
- android从放弃到精通 第三天 我还活着
- 我的android(第十三天)
- Android中Toast显示时间的自定义
- Android中Toast显示时间的自定义
- Android中Toast显示时间的自定义
- 逻辑回归
- C#算法学习-->递归
- UITableView 如何修改数据
- uva11354 - Bond
- cygwin使用过程中下的各种记录
- 我的android 第三天 - 自定义Toast
- 如何终止线程的运行(C/C++)
- SMO
- uva 11136 Hoax or what
- 适配器模式
- 微信公众平台开放 9 大高级接口,它有什么意义?
- 浅析Android应用增量升级
- memcached的头文件
- 链队列