Android 自定义Toast

在Android中,默认的Toast样式很难看。而且,Toast存在一个小小的bug,就是后来的toast必须要等之前的toast消失后才能显现出来,不过有一个解决方案,见http://blog.csdn.net/czjuttsw/article/details/8274276

        

一般地,我喜欢用toast来打印一些测试信息,虽然更推荐使用LogCat。细心的人可能注意到,android的音量调节视图其实也是一个toast。

通常,我们想要显示一个toast,只需一个语句:

[java]  view plain copy
  1. Toast.makeText(context,text,duration).show();  

context代表上下文环境,text代表显示的内容,duration代表显示时长(Toast.LENGTH_LONG和Toast.LENGTH_SHORT)。除了这些,我们还能设置

位置(setGravity),设置View等。

下面将实现一个简单的自定义toast,喜欢的可以看一看。

主要原理是自定义View,然后调用toast.setView方法。

先看看自定义View的一个显示效果:


用到的happy pig 图片(pig.png):

-- ---------------------------------------------------------------

-- ------

-- ------

-- ---------------------------------------------------------------

步骤一:

定义一个简单的layout,命名为custom_toast.xml :

[html]  view plain copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:id="@+id/custom_toast_layout_id"  
  4.     android:layout_width="fill_parent"  
  5.     android:layout_height="fill_parent"  
  6.     android:orientation="horizontal"  
  7.     android:padding="5dp"  
  8.     android:background="@drawable/corner" >  
  9.   
  10.     <ImageView   
  11.         android:id="@+id/toastpic"  
  12.         android:layout_width="wrap_content"  
  13.         android:layout_height="wrap_content"  
  14.         android:src="@drawable/pig"/>  
  15.   
  16.     <TextView  
  17.         android:id="@+id/toasttext"  
  18.         android:layout_width="wrap_content"  
  19.         android:layout_height="wrap_content"  
  20.         android:textColor="#000"  
  21.         android:paddingTop="17sp" />  
  22.   
  23. </LinearLayout>  

步骤二:

由于自定义View的四个角是直角不好看,我们将它改变成圆角,给人感觉更加柔和。

在drawable文件夹下新建corner.xml :

[html]  view plain copy
  1. <?xml version="1.0" encoding="utf-8"?>   
  2. <shape xmlns:android="http://schemas.android.com/apk/res/android" >  
  3.   
  4.     <solid android:color="#ffffffff" />  
  5.     <corners android:radius="10dp" />  
  6.     <padding  
  7.         android:bottom="5dp"  
  8.         android:left="5dp"  
  9.         android:right="5dp"  
  10.         android:top="5dp" />  
  11.       
  12. </shape>  

步骤三:

在Activity里制作一个香喷喷的toast:

[java]  view plain copy
  1. public void makeCustomToast(String text,int duration) {  
  2.       
  3.     View layout = getLayoutInflater().inflate(R.layout.custom_toast,   
  4.             (ViewGroup) findViewById(R.id.custom_toast_layout_id));  
  5.        // set a message  
  6.        TextView toastText = (TextView) layout.findViewById(R.id.toasttext);  
  7.        toastText.setText(text);  
  8.   
  9.        // Toast...  
  10.        Toast toast = new Toast(this);  
  11.        toast.setGravity(Gravity.CENTER_VERTICAL, 00);  
  12.        toast.setDuration(duration);  
  13.        toast.setView(layout);  
  14.        toast.show();  
  15. }  

步骤四:

Run it!!!


参考资料来源于:

1. 实现圆角:http://zcligong.blog.163.com/blog/static/210699224201293114127133/

2. 自定义Toast: http://stackoverflow.com/questions/11288475/custom-toast-in-android-a-simple-example



本文地址: http://blog.csdn.net/tounaobun/article/details/8866845

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值