Toast遇到一次写一次好麻烦,所以弄成一个工具类,调用方便点。
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
import com.wsp.thinkpad.aa.R;
/**
* Created by ${吴心良}
* on 2017/2/17.
* description:
*/
public class ToastUtils {
private static Toast toast;
public static void showToast(final Context context, final String message, final int duration) {
toast = Toast.makeText(context.getApplicationContext(), message, duration);
toast.show();
}
public static void showIconToast(Context context, String textId, int iconId, int colorId) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.toast, null);
((TextView) layout).setText(textId);
((TextView) layout).setTextColor(context.getResources().getColor(colorId));
((TextView) layout).setCompoundDrawablesWithIntrinsicBounds(iconId, 0, 0, 0);
Toast toast = new Toast(context);
toast.setDuration(Toast.LENGTH_SHORT);
toast.setView(layout);
toast.show();
}
public static void showLongToast(final Context context, final String message) {
showToast(context, message, Toast.LENGTH_LONG);
}
public static void showShortToast(final Context context, final String message) {
showToast(context, message, Toast.LENGTH_SHORT);
}
}
下面是xml代码 想怎么设置就怎么设置
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:drawablePadding="12dp"
android:padding="12dp"
android:gravity="center"
android:background="#999999">
</TextView>