android自定义Toast之-弹出消息
实现方法:
1.new 一个Toast实例toast。
2.自定义一个显示的View实例view 。
3.把toast.setView(view),toast.setDuration(Toast.LENGTH_LONG)设置显消息示时间
4.避免操作有误一直重复弹出消息处理,定义一个Toast的全局变量避免重复实例化进行控制
下面是代码
package com.android.hexiang.otptoken; import android.view.Gravity; import android.view.LayoutInflater; import android.view.View; import android.content.Context; import android.widget.TextView; import android.widget.Toast; import com.android.hexiang.otptoken.R; /** * Created by hexiang on 2014/12/10. */ public class MyToast { private static Toast mToast = null; /** * 自定义消息框 * @param c * Created by hexiang on 2014/12/10. */ @SuppressWarnings("deprecation") private static Toast showCustomerToast(final Context c,String msg,int duration) { //获取LayoutInflater对象,该对象可以将布局文件转换成与之一致的view对象 LayoutInflater inflater=LayoutInflater.from(c); //将布局文件转换成相应的View对象 View layout=inflater.inflate(R.layout.my_customer_toast_layout,null); layout.setBackgroundDrawable(c.getResources().getDrawable(R.drawable.back_toast_style)); //从layout中按照id查找TextView对象 TextView textView=(TextView)layout.findViewById(R.id.txt_msg); //设置TextView的text内容 textView.setText(msg); //实例化一个Toast对象 Toast toast=new Toast(c.getApplicationContext()); toast.setDuration(duration); toast.setGravity(Gravity.BOTTOM|Gravity.CENTER_HORIZONTAL, 0, 160); toast.setView(layout); return toast; } /** * 长消息框 指的的是时间长短 * Created by hexiang on 2014/12/10. * @param c 上下文 * @param msg 消息内容 */ @SuppressWarnings("deprecation") public static void showCustomerToastLong(final Context c,String msg) { //避免重复弹出消息 if (mToast == null) { mToast =showCustomerToast(c,msg,Toast.LENGTH_LONG); } else { TextView txt_msg=(TextView) mToast.getView().findViewById(R.id.txt_msg); txt_msg.setText(msg); mToast.setDuration(Toast.LENGTH_LONG); } mToast.show(); } /** * 短消息框 指的的是时间长短 mToast.setDuration(Toast.LENGTH_SHORT); * @param c 上下文 * @param msg 消息内容 * Created by hexiang on 2014/12/10. */ @SuppressWarnings("deprecation") public static void showCustomerToastShot(final Context c,String msg) { //避免重复弹出消息 if (mToast == null) { mToast =showCustomerToast(c,msg,Toast.LENGTH_SHORT); } else { TextView txt_msg=(TextView) mToast.getView().findViewById(R.id.txt_msg); txt_msg.setText(msg); mToast.setDuration(Toast.LENGTH_SHORT); } mToast.show(); } }
下面是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:id="@+id/toast_layout_root" 6 android:orientation="vertical" > 7 8 <TextView 9 android:layout_margin="6dp" 10 android:layout_gravity="center_vertical|center_horizontal" 11 android:id="@+id/txt_msg" 12 android:layout_width="match_parent" 13 android:textColor="@color/white_light" 14 android:textSize="14sp" 15 android:text="this is a test toast" 16 android:layout_height="wrap_content" 17 /> 18 </LinearLayout>
下面是自定义渐变背景
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" > <gradient android:angle="315" android:centerX="0.5" android:centerY="0.5" android:centerColor="#fcdede" android:endColor="#e5edbe" android:startColor="#d2b2b2" /> <corners android:radius="3dp" /> </shape>
温习下android:shape的使用
gradient:渐变
android:startColor和android:endColor分别为起始和结束颜色,android:centerColor中间部分颜色
ndroid:angle是渐变角度,必须为45的整数倍。
最后是调用
1 MyToast.showCustomerToastLong(this,"测试消息框");
Created by hexiang on 2014/12/10. 清分依然