【android自定义控件】自定义Toast,AlterDialog,Notification 四
1、自定义Toast
有没有看见一下app有很漂亮的Toast呢,不在局限于黑色背景,今天我就来带你自定义一看自己的Toast吧
首先 我们需要new Toast();对象
然后 有了Toast对象之后就可以对Toast进行定制了,我们需要考虑,怎么样定制呢?一般定制控件,都是写一个layout的xml布局,就可以了
我们不例外,来定义个xml文件,(一个图标,跟一行title)
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:orientation="horizontal"android:layout_width="fill_parent"android:layout_height="fill_parent"android:background="#ffffff"android:gravity="center"><ImageView android:id="@+id/iv_icon"android:layout_width="wrap_content"android:layout_height="wrap_content"/><TextView android:id="@+id/tv_toast"android:layout_width="wrap_content"android:layout_height="wrap_content"/></LinearLayout>
定义完xml,就开始真正做了,通过layoutInflater加载器把布局文件加载进来
接下来你懂得
toast.setView(v);//加载View到toast
toast.setDuration(500);//持续时间
解释下这两行代码,为什么想到使用set方法呢,Toast不是提供很多属性吗?
mNextView属性什么,就是加载View,但是我们看了下源码,这个属性是私有的,没办法直接使用,那我们就可以在源码中跟着,他是怎么赋值的,
在千辛万苦的找寻后,发现Toast提供了mNextView的set方法,就是setView()。找到了,我们就直接按照这种方式使用就ok了
看看实现的代码:
public static void show(Context context,String str){Toast toast=new Toast(context);View v= LayoutInflater.from(context).inflate(R.layout.toast,null);TextView textView=(TextView)v.findViewById(R.id.tv_toast);ImageView imageView=(ImageView)v.findViewById(R.id.iv_icon);textView.setText(str);imageView.setBackgroundResource(R.drawable.ic_launcher);toast.setView(v);//加载View到toasttoast.setDuration(500);//持续时间toast.setGravity(Gravity.CENTER,0,0);//设置toast位置toast.show();}
注意:记得toast的最后一步,那就是toast.show().
2、自定义AlterDialog
1)创建LayoutInflater
LayoutInflater li=LayoutInflater.from(this);
2)填充布局
View quakeDetailsView=li.inflate(R.layout.quake_details, null);
3)创建AlertDialog
AlertDialog.Builder quakeDialog=new AlertDialog.Builder(this);
AlertDialog的构造函数都是protect的,android只提供了AlertDialog.Builder来构造AlertDialog
4)设置AlertDialog自定义视图
quakeDialog.setView(quakeDetailsView);
5)返回Dialog
quakeDialog.create();
写一个xml文件,定义Dialog的布局
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:orientation="vertical"android:layout_width="fill_parent"android:minWidth="280dip"android:layout_height="wrap_content"><LinearLayoutandroid:orientation="vertical"android:background="@drawable/header"android:layout_width="fill_parent"android:layout_height="wrap_content"><TextViewstyle="@style/DialogText.Title"android:id="@+id/title"android:paddingRight="8dip"android:paddingLeft="8dip"android:background="@drawable/title"android:layout_width="wrap_content"android:layout_height="wrap_content"/></LinearLayout><LinearLayoutandroid:id="@+id/content"android:orientation="vertical"android:background="@drawable/center"android:layout_width="fill_parent"android:layout_height="wrap_content"><TextViewstyle="@style/DialogText"android:id="@+id/message"android:padding="5dip"android:layout_width="fill_parent"android:layout_height="wrap_content"/></LinearLayout><LinearLayoutandroid:orientation="horizontal"android:background="@drawable/footer"android:layout_width="fill_parent"android:layout_height="wrap_content"><Buttonandroid:id="@+id/positiveButton"android:layout_marginTop="3dip"android:layout_width="0dip"android:layout_weight="1"android:layout_height="wrap_content"android:singleLine="true"/><Buttonandroid:id="@+id/negativeButton"android:layout_marginTop="3dip"android:layout_width="0dip"android:layout_weight="1"android:layout_height="wrap_content"android:singleLine="true"/></LinearLayout></LinearLayout>
定义好xml,就开始定义AlterDialog了
AlertDialog.Builder build = new AlertDialog.Builder(context);build.setIcon(R.drawable.ic_launcher);build.setTitle("Dialog Title");build.setCancelable(false); build.setIcon(R.drawable.ic_launcher);build.setView(view);build.setPositiveButton("Ok", new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) {// TODO Auto-generated method stubsetTitle(txt.getText());}});build.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) {// TODO Auto-generated method stubsetTitle("");}});build.show();
我就不给出完整代码了,原理最重要。
Dialog值得学习的就是它的设计模式----建造者模式,把一个个组件通过set方法组建起来,知道学习
3、自定义Notification
xml就不写了自己写吧,想定义什么就布局什么
说说Notification定义的步骤
1)需要一个NotificationManager通知管理器,这个管理器不是通过new得来的,是调用了系统的服务
//获取到系统的notificationManager
notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
2)notificationManager调用notify方法(我喜欢倒着写,有时候倒着写工具会帮我们提点写信息)
notificationManager.notify(0, notification);
看见这行代码 我们知道需要定义一个notification对象
3)定义notification对象
Notification notification = new Notification(R.drawable.icon, tickerText, when);
//R.drawable.icon notifition的图标,
//tickerText 通知的title
//when 弹出时间
定义好这些有没有发现少了些什么呢?
是的,只有弹出提示,没有用户点击notification后的内容,接下来我就定义一个
Intent intent = new Intent(this,Bactivity.class); PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0); notification.contentIntent = pendingIntent; //自定义界面 RemoteViews rv = new RemoteViews(getPackageName(), R.layout.noti_layout); rv.setTextViewText(R.id.tv_rv, "我是自定义的 notification"); rv.setProgressBar(R.id.pb_rv, 80, 20, false); notification.contentView = rv;
好了 大功告成
给一个整理的代码吧,然后在回顾下上面的步骤,是不是有些道理呢:
public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); //获取到系统的notificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); } public void click(View view ){ //实例化一个notification String tickerText = "IP号码 设置完毕"; long when = System.currentTimeMillis(); Notification notification = new Notification(R.drawable.icon, tickerText, when); //不能手动清理 //notification.flags= Notification.FLAG_NO_CLEAR; //添加音乐 //notification.sound = Uri.parse("/sdcard/haha.mp3"); //设置用户点击notification的动作 // pendingIntent 延期的意图 Intent intent = new Intent(this,Bactivity.class); PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0); notification.contentIntent = pendingIntent; //自定义界面 RemoteViews rv = new RemoteViews(getPackageName(), R.layout.noti_layout); rv.setTextViewText(R.id.tv_rv, "我是自定义的 notification"); rv.setProgressBar(R.id.pb_rv, 80, 20, false); notification.contentView = rv; //把定义的notification 传递给 notificationmanager notificationManager.notify(0, notification); }
}
【android自定义控件】自定义Toast,AlterDialog,Notification 四相关推荐
- Android例子—自定义Toast(吐司)样式
1.直接调用Toast类的makeText()方法创建 这是我们用的最多的一种形式了!比如点击一个按钮,然后弹出Toast,用法: Toast.makeText(MainActivity.this, ...
- android中自定义 toast,android 自定义Toast样式和显示方式
问题: 1.android 开发中如果不停的触发显示Toast,会造成Toast一个接一个的弹出,非常影响用户体验. 2.android设备有千万个,每个设备的Toast的背景有可能不一样,造成在应用 ...
- android中自定义 toast,android 自定义Toast
Toast是android的一个简易消息提示框. 它不会获得焦点,也无法被点击.向用户提示信息,却不停留着不动. 其实,自定义Toast非常简单: 先看效果: 首先:新建一个mtoast.xml布局文 ...
- Android开发之自定义Toast(带详细注释)
因为工作需求,所以自己研究了自定义Toast,这里做出总结: 在此之前有一点需要提前说明:Toast与其他组件一样,都属于UI界面中的内容,因此在子线程中无法使用Toast弹出提示内容,如果强行在子线 ...
- Android实例-手机安全卫士(四十一)-选择自定义Toast背景
一.目标 通过对话框选择并保存自定义的Toast背景 二.代码实现 1.复制layout文件夹中的model_setting_item.xml文件,以其为模板进行修改(取名为model_ ...
- Android Toast 设置到屏幕中间,自定义Toast的实现方法,及其说明
http://blog.csdn.net/wangfayinn/article/details/8065763 Android Toast用于在手机屏幕上向用户显示一条信息,一段时间后信息会自动消失. ...
- Android 自定义Toast显示(不限时+在其他应用之上显示)
自定义Toast显示(不限时+在其他应用之上显示) 一.首先写好自定义Toast的布局 toast_view.xml <?xml version="1.0" encoding ...
- android 自定义时钟,Android自定义控件之圆形时钟(续)
在上篇文章中,我向大家介绍了如何通过自定义View一步步画出一个漂亮的圆形时钟.如果你还没看的话,我不建议你接着往下看,因为这篇文章是接着上篇的文章,如果直接看的话可能会不知所云,所以还是建议你先看一 ...
- android自定义view获取控件,android 自定义控件View在Activity中使用findByViewId得到结果为null...
转载:http://blog.csdn.net/xiabing082/article/details/48781489 1. 大家常常自定义view,,然后在xml 中添加该view 组件..如果在 ...
- Android 自定义Toast实现多次触发只会显示一次toast
#使用场景描述 当我们处于某个场景,例如一个按钮可以触发toast的显示,当你在多次点击按钮时,会多次触发toast的显示.而调用android原生的toast的makeText的方式所生产的toas ...
最新文章
- 配置jdk环境 windows
- Read correction for non-uniform coverages 读校正非均匀覆盖
- Linux下VNC配置多个桌面和修改密码 不会当系统重启vnc失效
- 2048(lj模拟)
- php隐含值传递,php – jQuery更新隐藏的输入值,但不传递给POST变量
- 手写自己的MyBatis框架-MapperProxy
- mysql无法导入函数和存储过程解决方法
- char un 数组printf_c语言中能不能用printf函数直接输出数组?如printf(%d,a[3][3]);
- linux系统下安装和配置redis(2021版)
- LOGO设计没有灵感?5种方法来寻找标志设计的灵感和想法
- Java集合之LinkedHashMap
- GlusterFS集群文件系统
- 云计算8项核心技术分析
- 新辰:雕爷与张朝阳分享创业感悟 给90后创业者打鸡血共勉
- 旅游网站设计参考文献优秀范例合集
- hihocoder 网易游戏2016实习生招聘在线笔试 解题报告
- excel表格如何转换成word表格_Excel表格转换为Word表格?99%的人想不到这样做最简单!...
- 微型计算机不是ecu,ECU升级是什么意思?
- bilibili下载的m4s格式视频如何还原为mp4?
- ak和sk的意思及用法