前言:
一般提示都是用Toast.makeText提示,为了满足客服要求和大众化用最流行的一种顶部弹出提示框,提示还可以自带图片、
Toast.makeText(this, "提示消息", Toast.LENGTH_SHORT).show();
一:效果图、
二:实现步骤:
1.自定义Toast工具类、
package com.zjtd.bzcommunity.util;
import android.content.Context;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
import com.zjtd.bzcommunity.R;
import java.util.Timer;
import java.util.TimerTask;
/**
* Created by Administrator on 2017/2/17.
* Toast自定义工具类
*/
public class ToastCommom {
private static TextView text;
private static Toast toast;
/**
* 显示Toast
* @param context
* @param tvString
* @param cntime
*/
public static void ToastShow(Context context, String tvString, int cntime) {
if (toast == null) {
toast = new Toast(context);
toast.setGravity(Gravity.TOP | Gravity.FILL_HORIZONTAL, 0, 0);
toast.setDuration(Toast.LENGTH_LONG);
View layout = LayoutInflater.from(context).inflate(R.layout.toast_xml, null);
// ImageView mImageView = (ImageView) layout.findViewById(R.id.iv);
// mImageView.setBackgroundResource(R.drawable.ic_launcher);
text = (TextView) layout.findViewById(R.id.text);
toast.setView(layout);
showMyToast(toast, cntime);
}
text.setText(tvString);
text.setTextColor(0xFFFFFFFF);
text.setTextSize(16);
if(toast!=null){
toast.show();
}
}
//自定义停留时间
public static void showMyToast(final Toast toast, final int cnt) {
final Timer timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
toast.show();
}
}, 0, 3000);
new Timer().schedule(new TimerTask() {
@Override
public void run() {
toast.cancel();
timer.cancel();
}
}, cnt);
}
}
2.工具类里面需要的xml布局、
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/toast_layout_root"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#90000000"
android:orientation="horizontal"
android:padding="10dp">
<ImageView
android:id="@+id/iv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="8dp" />
<TextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
3.activity或者fragment的引用、
//农场菜金
case R.id.caijin_id:
ToastCommom.ToastShow(getActivity(),"农场菜金",3000);
break;
-------------就这么多,自己感觉这种蛮流行和大众化的,大神勿喷哦、