zoukankan      html  css  js  c++  java
  • Android之Toast通知的几种自定义用法

    Toast在手机屏幕上向用户显示一条信息,一段时间后信息会自动消失。

    1.默认用法

    1. Toast.makeText(getApplicationContext(), "默认Toast样式",Toast.LENGTH_SHORT).show();  

    2.Fragment中的用法

    1. Toast.makeText(getActivity(),"网络连接错误,请检察网络设置", Toast.LENGTH_LONG).show();  

    3.自定义显示位置效果

    1. toast = Toast.makeText(getApplicationContext(), "自定义位置Toast", Toast.LENGTH_LONG);  
    2. toast.setGravity(Gravity.CENTER, 0, 0);  
    3. toast.show();  

    4.带图片效果

    1. toast = Toast.makeText(getApplicationContext(), "带图片的Toast", Toast.LENGTH_LONG);  
    2. toast.setGravity(Gravity.CENTER, 0, 0);  
    3. LinearLayout toastView = (LinearLayout) toast.getView();  
    4. ImageView imageCodeProject = new ImageView(getApplicationContext());  
    5. imageCodeProject.setImageResource(R.drawable.icon);  
    6. toastView.addView(imageCodeProject, 0);  
    7. toast.show();  

    5.完全自定义效果

    1. LayoutInflater inflater = getLayoutInflater();  
    2. View layout = inflater.inflate(R.layout.custom,(ViewGroup) findViewById(R.id.llToast));  
    3. ImageView image = (ImageView) layout.findViewById(R.id.tvImageToast);  
    4. image.setImageResource(R.drawable.icon);  
    5. TextView title = (TextView) layout.findViewById(R.id.tvTitleToast);  
    6. title.setText("Attention");  
    7. TextView text = (TextView) layout.findViewById(R.id.tvTextToast);  
    8. text.setText("完全自定义Toast");  
    9. toast = new Toast(getApplicationContext());  
    10. toast.setGravity(Gravity.RIGHT | Gravity.TOP, 12, 40);  
    11. toast.setDuration(Toast.LENGTH_LONG);  
    12. toast.setView(layout);  
    13. toast.show();  

    6.其他线程

    Main.java打码
    1. package com.wjq.toast;  
    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.ViewGroup;  
    10. import android.view.View.OnClickListener;  
    11. import android.widget.ImageView;  
    12. import android.widget.LinearLayout;  
    13. import android.widget.TextView;  
    14. import android.widget.Toast;  
    15.   
    16. public class Main extends Activity implements OnClickListener {  
    17.     Handler handler = new Handler();  
    18.   
    19.     @Override  
    20.     public void onCreate(Bundle savedInstanceState) {  
    21.         super.onCreate(savedInstanceState);  
    22.         setContentView(R.layout.main);  
    23.   
    24.         findViewById(R.id.btnSimpleToast).setOnClickListener(this);  
    25.         findViewById(R.id.btnSimpleToastWithCustomPosition).setOnClickListener(this);  
    26.         findViewById(R.id.btnSimpleToastWithImage).setOnClickListener(this);  
    27.         findViewById(R.id.btnCustomToast).setOnClickListener(this);  
    28.         findViewById(R.id.btnRunToastFromOtherThread).setOnClickListener(this);  
    29.   
    30.     }  
    31.   
    32.     public void showToast() {  
    33.         handler.post(new Runnable() {  
    34.   
    35.             @Override  
    36.             public void run() {  
    37.                 Toast.makeText(getApplicationContext(), "我来自其他线程!",Toast.LENGTH_SHORT).show();  
    38.             }  
    39.         });  
    40.     }  
    41.   
    42.     @Override  
    43.     public void onClick(View v) {  
    44.         Toast toast = null;  
    45.         switch (v.getId()) {  
    46.         case R.id.btnSimpleToast:  
    47.             Toast.makeText(getApplicationContext(), "默认Toast样式",Toast.LENGTH_SHORT).show();  
    48.             break;  
    49.         case R.id.btnSimpleToastWithCustomPosition:  
    50.             toast = Toast.makeText(getApplicationContext(), "自定义位置Toast",Toast.LENGTH_LONG);  
    51.             toast.setGravity(Gravity.CENTER, 0, 0);  
    52.             toast.show();  
    53.             break;  
    54.         case R.id.btnSimpleToastWithImage:  
    55.             toast = Toast.makeText(getApplicationContext(), "带图片的Toast",Toast.LENGTH_LONG);  
    56.             toast.setGravity(Gravity.CENTER, 0, 0);  
    57.             LinearLayout toastView = (LinearLayout) toast.getView();  
    58.             ImageView imageCodeProject = new ImageView(getApplicationContext());  
    59.             imageCodeProject.setImageResource(R.drawable.icon);  
    60.             toastView.addView(imageCodeProject, 0);  
    61.             toast.show();  
    62.             break;  
    63.         case R.id.btnCustomToast:  
    64.             LayoutInflater inflater = getLayoutInflater();  
    65.             View layout = inflater.inflate(R.layout.custom,(ViewGroup) findViewById(R.id.llToast));  
    66.             ImageView image = (ImageView) layout.findViewById(R.id.tvImageToast);  
    67.             image.setImageResource(R.drawable.icon);  
    68.             TextView title = (TextView) layout.findViewById(R.id.tvTitleToast);  
    69.             title.setText("Attention");  
    70.             TextView text = (TextView) layout.findViewById(R.id.tvTextToast);  
    71.             text.setText("完全自定义Toast");  
    72.             toast = new Toast(getApplicationContext());  
    73.             toast.setGravity(Gravity.RIGHT | Gravity.TOP, 12, 40);  
    74.             toast.setDuration(Toast.LENGTH_LONG);  
    75.             toast.setView(layout);  
    76.             toast.show();  
    77.             break;  
    78.         case R.id.btnRunToastFromOtherThread:  
    79.             new Thread(new Runnable() {  
    80.                 public void run() {  
    81.                     showToast();  
    82.                 }  
    83.             }).start();  
    84.             break;  
    85.         }  
    86.     }  
    87. }  
    main.xml代码
    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:gravity="center"  
    6.     android:orientation="vertical"  
    7.     android:padding="5dip" >  
    8.   
    9.     <Button  
    10.         android:id="@+id/btnSimpleToast"  
    11.         android:layout_width="fill_parent"  
    12.         android:layout_height="wrap_content"  
    13.         android:text="默认" >  
    14.     </Button>  
    15.   
    16.     <Button  
    17.         android:id="@+id/btnSimpleToastWithCustomPosition"  
    18.         android:layout_width="fill_parent"  
    19.         android:layout_height="wrap_content"  
    20.         android:text="自定义显示位置" >  
    21.     </Button>  
    22.   
    23.     <Button  
    24.         android:id="@+id/btnSimpleToastWithImage"  
    25.         android:layout_width="fill_parent"  
    26.         android:layout_height="wrap_content"  
    27.         android:text="带图片" >  
    28.     </Button>  
    29.   
    30.     <Button  
    31.         android:id="@+id/btnCustomToast"  
    32.         android:layout_width="fill_parent"  
    33.         android:layout_height="wrap_content"  
    34.         android:text="完全自定义" >  
    35.     </Button>  
    36.   
    37.     <Button  
    38.         android:id="@+id/btnRunToastFromOtherThread"  
    39.         android:layout_width="fill_parent"  
    40.         android:layout_height="wrap_content"  
    41.         android:text="其他线程" >  
    42.     </Button>  
    43.   
    44. </LinearLayout>  
    custom.xml
    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>   1 /**
       2 *另一种封装方式
       3 *
       4 */
       5 import android.content.Context;
       6 import android.view.Gravity;
       7 import android.view.LayoutInflater;
       8 import android.view.View;
       9 import android.view.ViewGroup;
      10 import android.widget.ImageView;
      11 import android.widget.LinearLayout;
      12 import android.widget.TextView;
      13 import android.widget.Toast;
      14 
      15 import com.letv.recorder.R;
      16 
      17 /**
      18  * Created by juyanming on 16/7/4.
      19  */
      20 public class ToastUtils {
      21 
      22     /**
      23      * 描述:系统Toast
      24      * @param context 应用上下文
      25      * @param str 消息框提示的文本内容
      26      */
      27     public void t_default(Context context,String str){
      28         Toast.makeText(context, str,
      29                 Toast.LENGTH_SHORT).show();
      30     }
      31 
      32     /**
      33      * 描述:自定义图片Toast
      34      * @param context 应用上下文
      35      * @param str   消息框提示的文本内容
      36      * @param imgId 图片id
      37      */
      38     public void t_img(Context context,String str,int imgId){
      39         Toast toast = Toast.makeText(context,
      40                 "带图片的Toast", Toast.LENGTH_LONG);
      41         toast.setGravity(Gravity.CENTER, 0, 0);
      42         LinearLayout toastView = (LinearLayout) toast.getView();
      43         ImageView imageCodeProject = new ImageView(context);
      44         imageCodeProject.setImageResource(imgId);
      45         toastView.addView(imageCodeProject, 0);
      46         toast.show();
      47     }
      48 
      49     /**
      50      *
      51      * @param context 应用上下文
      52      * @param str 消息框提示的文本内容
      53      * @param custom    布局
      54      * @param llToast  viewId
      55      * @param icon 图片id
      56      * @param tvImageToast  视图id
      57      * @param tvTitleToast
      58      * @param tvTextToast
      59      */
      60     public void t_complete(Context context,String str,int custom,int llToast,int icon,int tvImageToast,int tvTitleToast,int tvTextToast){
      61         LayoutInflater inflater = LayoutInflater.from(context);
      62         View layout = inflater.inflate(custom,
      63                 (ViewGroup) findViewById(R.id.llToast));
      64         ImageView image = (ImageView) layout
      65                 .findViewById(tvImageToast);
      66         image.setImageResource(icon);
      67         TextView title = (TextView) layout.findViewById(tvTitleToast);
      68         title.setText("Attention");
      69         TextView text = (TextView) layout.findViewById(tvTextToast);
      70         text.setText("完全自定义Toast");
      71         Toast toast = new Toast(context);
      72         toast.setGravity(Gravity.RIGHT | Gravity.TOP, 12, 40);
      73         toast.setDuration(Toast.LENGTH_LONG);
      74         toast.setView(layout);
      75         toast.show();
      76     }
      77 }
  • 相关阅读:
    UDP
    TCP
    python基础之socket编程
    单列模式
    元类
    issubclass()和isinstance()
    手持机设备公司(WINCE/ANDROID/LINUX)
    Android Hal 分析
    Android JNI 使用的数据结构JNINativeMethod详解
    MTK GPIO 一些理解
  • 原文地址:https://www.cnblogs.com/jym-sunshine/p/5641216.html
Copyright © 2011-2022 走看看