码迷,mamicode.com
首页 > 移动开发 > 详细

android 自定义Toast显示风格

时间:2015-08-12 00:58:41      阅读:252      评论:0      收藏:0      [点我收藏+]

标签:

1.创建一个自己想要显示Toast风格的XML如下代码(toast_xml.xml):

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:orientation="horizontal"   
  6.     android:padding="8dp"                
  7.     android:background="#DAAA"  
  8.     android:id="@+id/toast_layout_root">  
  9.       
  10. <ImageView   
  11.     android:layout_width="wrap_content"  
  12.     android:layout_height="wrap_content"  
  13.     android:layout_marginRight="8dp"  
  14.     android:id="@+id/iv"  
  15.     />  
  16. <TextView android:id="@+id/text"  
  17.     android:layout_width="wrap_content"  
  18.     android:layout_height="wrap_content"  
  19.     android:textColor="#FFF"  
  20.     />  
  21. </LinearLayout>  

2.封装一个Toast共同类,当然也可以直接在activity运用,那么我是用共通类,当然只是简单的封装:

  1. import com.sbr.activity.R;  
  2.   
  3. import android.annotation.SuppressLint;  
  4. import android.content.Context;  
  5. import android.view.Gravity;  
  6. import android.view.LayoutInflater;  
  7. import android.view.View;  
  8. import android.view.ViewGroup;  
  9. import android.widget.ImageView;  
  10. import android.widget.TextView;  
  11. import android.widget.Toast;  
  12.   
  13. @SuppressLint("ResourceAsColor")  
  14. public class ToastCommom {  
  15.       
  16.     private static ToastCommom toastCommom;  
  17.       
  18.     private Toast toast;  
  19.       
  20.     private ToastCommom(){  
  21.     }  
  22.       
  23.     public static ToastCommom createToastConfig(){  
  24.         if (toastCommom==null) {  
  25.             toastCommom = new ToastCommom();  
  26.         }  
  27.         return toastCommom;  
  28.     }  
  29.       
  30.     /**  
  31.      * 显示Toast  
  32.      * @param context  
  33.      * @param root  
  34.      * @param tvString  
  35.      */  
  36.       
  37.     public void ToastShow(Context context,ViewGroup root,String tvString){  
  38.         View layout = LayoutInflater.from(context).inflate(R.layout.toast_xml,root);  
  39.         TextView text = (TextView) layout.findViewById(R.id.text);  
  40.         ImageView mImageView = (ImageView) layout.findViewById(R.id.iv);  
  41.         mImageView.setBackgroundResource(R.drawable.ic_launcher);  
  42.         text.setText(tvString);  
  43.         text.setTextColor(R.color.aqua);  
  44.         toast = new Toast(context);  
  45.         toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);  
  46.         toast.setDuration(Toast.LENGTH_LONG);  
  47.         toast.setView(layout);  
  48.         toast.show();  
  49.     }  
  50.   
  51. }  

3.创建一个Activity去引用该共通类:

  1. import com.sbr.commonView.ToastCommom;  
  2.   
  3. import android.app.Activity;  
  4. import android.os.Bundle;  
  5. import android.view.View;  
  6. import android.view.View.OnClickListener;  
  7. import android.view.ViewGroup;  
  8. import android.widget.Button;  
  9.   
  10. public class ToastActivity extends Activity {  
  11.       
  12.     private Button mbutton;  
  13.       
  14.     private ToastCommom toastCommom;  
  15.   
  16.     @Override  
  17.     protected void onCreate(Bundle savedInstanceState) {  
  18.         // TODO Auto-generated method stub  
  19.         super.onCreate(savedInstanceState);  
  20.         setContentView(R.layout.toast_buton);  
  21.         toastCommom = ToastCommom.createToastConfig();  
  22.         mbutton = (Button) findViewById(R.id.btn);  
  23.         mbutton.setOnClickListener(new OnClickListener() {  
  24.               
  25.             @Override  
  26.             public void onClick(View v) {  
  27.                 // TODO Auto-generated method stub  
  28.                 toastCommom.ToastShow(ToastActivity.this, (ViewGroup)findViewById(R.id.toast_layout_root), "你好");  
  29.             }  
  30.         });  
  31.           
  32.     }  
  33.       
  34. }  


好了,一个简单的自定义toast显示就完成了;当然万变不离其宗,都是类似的做法。

android 自定义Toast显示风格

标签:

原文地址:http://www.cnblogs.com/exmyth/p/4722803.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!