android Toast五种特效

本文深入解析Android Toast的自定义效果,包括默认显示、自定义位置、带图片显示和完全自定义效果,并通过代码示例进行演示。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Toast是一种提供给用户简洁信息的视图。Toast类帮助你创建和显示该信息。

该视图已浮于应用程序之上的形式呈现给用户。因为它并不获得焦点,即使用户正在输入什么也不会受到影响。它的目标是尽可能已不显眼的方式,使用户看到你提供的信息。有两个例子就是音量控制和设置信息保存成功。

使用该类最简单的方法就是调用一个静态方法,让他来构造你需要的一切并返回一个新的 Toast 对象。

1、我们首先来看看Toast常用 的默认效果:


2、我们还可以自定义位置:


3、带图片的:


4、完全实现我们自己的自定义效果:


5、可以由其它线程更新:



查看源代码:

HelloToastActivity.java

[java]  view plain copy print ?
  1. package hb.android.hellotoast;  
  2.   
  3. import android.app.Activity;  
  4. import android.os.Bundle;  
  5. import android.os.Handler;  
  6. import android.view.Gravity;  
  7. import android.view.LayoutInflater;  
  8. import android.view.View;  
  9. import android.view.View.OnClickListener;  
  10. import android.widget.Button;  
  11. import android.widget.ImageView;  
  12. import android.widget.LinearLayout;  
  13. import android.widget.TextView;  
  14. import android.widget.Toast;  
  15.   
  16. public class HelloToastActivity extends Activity {  
  17.     /** Called when the activity is first created. */  
  18.     Button btn_default;  
  19.     Button btn_define;  
  20.     Button btn_all_define;  
  21.     Button btn_image_define;  
  22.     Button btn_other_thread;  
  23.   
  24.     @Override  
  25.     public void onCreate(Bundle savedInstanceState) {  
  26.         super.onCreate(savedInstanceState);  
  27.         setContentView(R.layout.main);  
  28.         initButton();  
  29.         btn_all_define.setOnClickListener(new MyOnClickListerer());  
  30.         btn_define.setOnClickListener(new MyOnClickListerer());  
  31.         btn_other_thread.setOnClickListener(new MyOnClickListerer());  
  32.         btn_image_define.setOnClickListener(new MyOnClickListerer());  
  33.         btn_default.setOnClickListener(new MyOnClickListerer());  
  34.     }  
  35.   
  36.     public void initButton() {  
  37.         btn_all_define = (Button) findViewById(R.id.btn_all_define);  
  38.         btn_default = (Button) findViewById(R.id.btn_default);  
  39.         btn_define = (Button) findViewById(R.id.btn_define);  
  40.         btn_image_define = (Button) findViewById(R.id.btn_image_define);  
  41.         btn_other_thread = (Button) findViewById(R.id.btn_other_thread);  
  42.     }  
  43.   
  44.     private class MyOnClickListerer implements OnClickListener {  
  45.           
  46.         Handler handler = new Handler();  
  47.   
  48.         @Override  
  49.         public void onClick(View v) {  
  50.             if (v == btn_default) {  
  51.                 Toast.makeText(getApplicationContext(), "这 是默认效果",  
  52.                         Toast.LENGTH_SHORT).show();  
  53.             } else if (v == btn_define) {  
  54.                 Toast toast = Toast.makeText(getApplicationContext(),  
  55.                         "这是自定义位置", Toast.LENGTH_SHORT);  
  56.                 toast.setGravity(Gravity.CENTER, 00);  
  57.                 toast.show();  
  58.             } else if (v == btn_image_define) {  
  59.                 Toast toast = Toast.makeText(getApplicationContext(), "这是带图片的",  
  60.                         Toast.LENGTH_SHORT);  
  61.                 LinearLayout toastView = (LinearLayout) toast.getView();  
  62.                 ImageView imageCodeProject = new ImageView(  
  63.                         getApplicationContext());  
  64.                 imageCodeProject.setImageResource(R.drawable.ic_launcher);  
  65.                 toastView.addView(imageCodeProject, 0);  
  66.                 toast.show();  
  67.             } else if (v == btn_all_define) {  
  68.                 LayoutInflater inflater = getLayoutInflater();  
  69.                 View view = inflater.inflate(R.layout.custom, null);  
  70.                 ImageView iv = (ImageView) view.findViewById(R.id.tvImageToast);  
  71.                 iv.setImageResource(R.drawable.ic_launcher);  
  72.                 TextView title = (TextView) view  
  73.                         .findViewById(R.id.tvTitleToast);  
  74.                 title.setText("Attention");  
  75.                 TextView text = (TextView) view.findViewById(R.id.tvTextToast);  
  76.                 text.setText("完全自定义Toast");  
  77.                 Toast toast = new Toast(getApplicationContext());  
  78.                 toast.setGravity(Gravity.RIGHT | Gravity.TOP, 1240);  
  79.                 toast.setDuration(Toast.LENGTH_LONG);  
  80.                 toast.setView(view);  
  81.                 toast.show();  
  82.             } else if (v == btn_other_thread) {  
  83.                 new Thread(new Runnable() {  
  84.                     public void run() {  
  85.                         System.out.println("d");  
  86.                         showToast();  
  87.                     }  
  88.                 }).start();  
  89.             }  
  90.         }  
  91.   
  92.         public void showToast() {  
  93.             handler.post(new Runnable() {  
  94.                 @Override  
  95.                 public void run() {  
  96.                     Toast.makeText(getApplicationContext(), "我来自其他线程!",  
  97.                             Toast.LENGTH_SHORT).show();  
  98.                 }  
  99.             });  
  100.         }  
  101.   
  102.     }  
  103. }  

custom.xml

[html]  view plain copy print ?
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:id="@+id/llToast"  
  4.     android:layout_width="wrap_content"  
  5.     android:layout_height="wrap_content"  
  6.     android:background="#ffffffff"  
  7.     android:orientation="vertical" >  
  8.   
  9.     <TextView  
  10.         android:id="@+id/tvTitleToast"  
  11.         android:layout_width="fill_parent"  
  12.         android:layout_height="wrap_content"  
  13.         android:layout_margin="1dip"  
  14.         android:background="#bb000000"  
  15.         android:gravity="center"  
  16.         android:textColor="#ffffffff" />  
  17.   
  18.     <LinearLayout  
  19.         android:id="@+id/llToastContent"  
  20.         android:layout_width="wrap_content"  
  21.         android:layout_height="wrap_content"  
  22.         android:layout_marginBottom="1dip"  
  23.         android:layout_marginLeft="1dip"  
  24.         android:layout_marginRight="1dip"  
  25.         android:background="#44000000"  
  26.         android:orientation="vertical"  
  27.         android:padding="15dip" >  
  28.   
  29.         <ImageView  
  30.             android:id="@+id/tvImageToast"  
  31.             android:layout_width="wrap_content"  
  32.             android:layout_height="wrap_content"  
  33.             android:layout_gravity="center" />  
  34.   
  35.         <TextView  
  36.             android:id="@+id/tvTextToast"  
  37.             android:layout_width="wrap_content"  
  38.             android:layout_height="wrap_content"  
  39.             android:gravity="center"  
  40.             android:paddingLeft="10dip"  
  41.             android:paddingRight="10dip"  
  42.             android:textColor="#ff000000" />  
  43.     </LinearLayout>  
  44.   
  45. </LinearLayout>  

main.xml

[html]  view plain copy print ?
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent"  
  5.     android:orientation="vertical"   
  6.     android:gravity="center_horizontal">  
  7.   
  8.     <TextView  
  9.         android:layout_width="fill_parent"  
  10.         android:layout_height="wrap_content"  
  11.         android:text="建立属于你自己的Toast" />  
  12.       
  13.     <Button   
  14.         android:id="@+id/btn_default"  
  15.         android:text="默认"  
  16.         android:layout_width="match_parent"  
  17.         android:layout_height="wrap_content"  
  18.         android:gravity="center_horizontal"/>  
  19.       
  20.     <Button   
  21.         android:id="@+id/btn_define"  
  22.         android:text="自定义位置"  
  23.         android:layout_width="match_parent"  
  24.         android:layout_height="wrap_content"  
  25.         android:gravity="center_horizontal"/>  
  26.       
  27.     <Button   
  28.         android:id="@+id/btn_image_define"  
  29.         android:text="带图片"  
  30.         android:layout_width="match_parent"  
  31.         android:layout_height="wrap_content"  
  32.         android:gravity="center_horizontal"/>  
  33.       
  34.     <Button   
  35.         android:id="@+id/btn_all_define"  
  36.         android:text="完全自定义效果"  
  37.         android:layout_width="match_parent"  
  38.         android:layout_height="wrap_content"  
  39.         android:gravity="center_horizontal"/>  
  40.       
  41.      <Button   
  42.         android:id="@+id/btn_other_thread"  
  43.         android:text="来自其它纯种"  
  44.         android:layout_width="match_parent"  
  45.         android:layout_height="wrap_content"  
  46.         android:gravity="center_horizontal"/>  
  47. </LinearLayout>  

源码下载:Android  Toast用法详解(各种自定义Toast)


ref:http://blog.youkuaiyun.com/huangbiao86/article/details/6965669


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值