不多說,上代碼
/**
* 高級toast功能,專治設計師到處Toast
* @param context
* @param msg
* @param view //在哪個view下面顯示這個Toast
* @param duration //顯示時長
*/
public static void showToastDialog(Context context,final String msg,View view,int duration){
showToastDialog(context, msg, view, 0, 0, duration);
}
/**
*
* @param xoff //x方向偏移
* @param yoff //y方向偏移
*/
public static void showToastDialog(Context context,final String msg,View view,int xoff,int yoff,int duration){
View contentView =LayoutInflater.from(context).inflate(R.layout.toast_view, null);
final PopupWindow pop = new PopupWindow(contentView, LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
pop.setBackgroundDrawable(context.getResources().getDrawable(R.drawable.toast_background));
pop.showAsDropDown(view,xoff,yoff);
TextView toast_msg=(TextView) contentView.findViewById(R.id.toast_msg);
toast_msg.setText(msg);
Handler handler=new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
pop.dismiss();
}
}, duration);
}
對應一個toast_view,可以自定義,我這邊弄了一個簡單的
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="400dp"
android:layout_height="50dp"
android:layout_gravity="center_horizontal"
android:background="@drawable/toast_background"
android:orientation="vertical" >
<TextView
android:id="@+id/toast_msg"
android:layout_width="400dp"
android:layout_height="50dp"
android:text="dklfj"
android:gravity="center"
android:textColor="#ffffff"
android:textSize="30sp" >
</TextView>
</RelativeLayout>
背景也可以自己寫一個,我這個設置了一個透明度,看着跟設計師的挺像的
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<!-- 邊緣線的寬度和顏色 -->
<stroke
android:width="1px"
android:color="#55000000" />
<!-- 中間的背景色 -->
<solid android:color="#55000000" />
<!-- 設置四個角的角度 -->
<corners
android:bottomLeftRadius="3dp"
android:bottomRightRadius="3dp"
android:topLeftRadius="3dp"
android:topRightRadius="3dp" />
</shape>
完成了,接下來就是使用了,到處使用,一行代碼搞定
DialogUtil.showToastDialog(mActivity, "測試一下效果,保佑2222", amountET,-80,150, 5000);
完成,笑一個。
過了幾天了,發現設計又異想天開了,又想隨時換背景顏色,又想換位置換控件大小,又想在提示文字左邊加個圖標,哎呀我去,換吧,再封裝個更完善的類來吧
import android.content.Context;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager.LayoutParams;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.TextView;
public class MyToast {
private PopupWindow pop;
private View contentView ;
private Context context;
private String msg;
private int toast_background=R.drawable.toast_background;
public MyToast(Context context, String msg) {
super();
this.context = context;
this.msg = msg;
initToast();
}
public MyToast(Context context) {
super();
this.context = context;
initToast();
}
public void setBackground(int background){
toast_background=background;
pop.setBackgroundDrawable(context.getResources().getDrawable(toast_background));
}
public void setSize(int width,int height){
pop.setWidth(width);
pop.setHeight(height);
}
public void changeTextColor(int color){
((TextView) contentView.findViewById(R.id.toast_msg)).setTextColor(color);
}
public void changeTextSize(int size){
((TextView) contentView.findViewById(R.id.toast_msg)).setTextSize(size);
}
public void changeTextMsg(int size,int color,String msg){
((TextView) contentView.findViewById(R.id.toast_msg)).setText(msg);
}
public void changeImg(int img){
ImageView imageView=(ImageView)contentView.findViewById(R.id.toast_img);
imageView.setImageResource(img);
imageView.setVisibility(View.VISIBLE);
}
private void initToast() {
contentView =LayoutInflater.from(context).inflate(R.layout.toast_view, null);
pop = new PopupWindow(contentView, LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
pop.setBackgroundDrawable(context.getResources().getDrawable(toast_background));
TextView toast_msg=(TextView) contentView.findViewById(R.id.toast_msg);
toast_msg.setText(msg);
}
public void show(View view,int duration){
show(view, 0,0,duration);
}
public void show(View view,int xoff,int yoff,int duration){
pop.showAsDropDown(view,xoff,yoff);
Handler handler=new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
pop.dismiss();
}
}, duration);
}
/**
* 高級toast功能,專治設計師到處Toast
* @param context
* @param msg
* @param view //在哪個view下面顯示這個Toast
* @param duration //顯示時長
*/
public static void showToastDialog(Context context,final String msg,View view,int duration){
showToastDialog(context, msg, view, 0, 0, duration,R.drawable.toast_background);
}
public static void showToastDialog(Context context,final String msg,View view,int xoff,int yoff,int duration){
showToastDialog(context, msg, view, xoff, yoff, duration,R.drawable.toast_background);
}
/**
*
* @param xoff //x方向偏移
* @param yoff //y方向偏移
*/
public static void showToastDialog(Context context,final String msg,View view,int xoff,int yoff,int duration,int background){
View contentView =LayoutInflater.from(context).inflate(R.layout.toast_view, null);
final PopupWindow pop = new PopupWindow(contentView, LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
pop.setBackgroundDrawable(context.getResources().getDrawable(background));
pop.showAsDropDown(view,xoff,yoff);
TextView toast_msg=(TextView) contentView.findViewById(R.id.toast_msg);
toast_msg.setText(msg);
Handler handler=new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
pop.dismiss();
}
}, duration);
}
}
想直接用之前的靜態方法也行,想變得多一點就用new,隨時控制位置顏色大小圖標什麼的。要顯示的時候調用show方法即可
注意:記得在Activity Onpause的時候dismiss掉哦