【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到toast
        toast.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">

    <LinearLayout
        android:orientation="vertical"
        android:background="@drawable/header"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content">

        <TextView
            style="@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>

    <LinearLayout
        android:id="@+id/content"
        android:orientation="vertical"
        android:background="@drawable/center"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content">
        <TextView
            style="@style/DialogText"
            android:id="@+id/message"
            android:padding="5dip"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"/>

    </LinearLayout>
    <LinearLayout
        android:orientation="horizontal"
        android:background="@drawable/footer"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content">

  
        <Button
            android:id="@+id/positiveButton"
            android:layout_marginTop="3dip"
            android:layout_width="0dip"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            android:singleLine="true"/>

        <Button
            android: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() {
         
        @Override
        public void onClick(DialogInterface dialog, int which) {
            // TODO Auto-generated method stub
            setTitle(txt.getText());
        }
    });
    build.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
         
        @Override
        public void onClick(DialogInterface dialog, int which) {
            // TODO Auto-generated method stub
            setTitle("");
        }
    });
    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);  
    }  
}  







评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值