下面列出了android.widget.Toast#setView ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* 一种自定义的Toast
*
* @param toastStr 字符串
*/
public void showToast(Context context, String toastStr) {
if (null != lastToast) {
lastToast.cancel();
}
// 初始化ToastView
LayoutInflater inflater = LayoutInflater.from(context.getApplicationContext());
View toastView = inflater.inflate(R.layout.toast_layout, null);
TextView toastTv = toastView.findViewById(R.id.toast_tv);
toastTv.setText(toastStr);
// 初始化Toast
Toast toast = new Toast(context.getApplicationContext());
toast.setDuration(Toast.LENGTH_SHORT);
toast.setView(toastView);
toast.setGravity(Gravity.BOTTOM, 0, Utils.dp2px(context.getApplicationContext(), 30));
toast.show();
lastToast = toast;
}
private void showStartupInfo() {
if (swipeInfoNotNeeded) {
//show info only if user didn't swipe already.
return;
}
TextView tv = new TextView(this);
tv.setText("Swipe left/right to switch between reports!");
tv.setTextColor(Color.CYAN);
tv.setTextSize(28);
for (int i = 0; i < 2; i++) {
//Show toast twice the "long" period
Toast toast = new Toast(getApplicationContext());
toast.setView(tv);
toast.setGravity(Gravity.CENTER, 0, 0);
toast.show();
}
}
private void b(String s1, int i1)
{
Toast toast = Toast.makeText(this, s1, 1);
LinearLayout linearlayout = (LinearLayout)toast.getView();
((TextView)linearlayout.getChildAt(0)).setPadding(8, 0, 0, 0);
ImageView imageview = new ImageView(this);
imageview.setLayoutParams(new android.widget.LinearLayout.LayoutParams(com.tencent.connect.avatar.c.a(this, 16F), com.tencent.connect.avatar.c.a(this, 16F)));
if (i1 == 0)
{
imageview.setImageDrawable(b("com.tencent.plus.ic_success.png"));
} else
{
imageview.setImageDrawable(b("com.tencent.plus.ic_error.png"));
}
linearlayout.addView(imageview, 0);
linearlayout.setOrientation(0);
linearlayout.setGravity(17);
toast.setView(linearlayout);
toast.setGravity(17, 0, 0);
toast.show();
}
public static void Toast_MSG(Context context, String msg) {
Toast toa = new Toast(context);
toa.setDuration(Toast.LENGTH_SHORT);
View tView = LayoutInflater.from(context).inflate(R.layout.msg_toast, null);
TextView tvmsg = (TextView) tView.findViewById(R.id.msg_toast_text);
tvmsg.setText(msg);
toa.setView(tView);
toa.show();
}
public static void showToast(@NonNull Context context, @NonNull String msg, int duration, int icon) {
if (!msg.isEmpty()) {
CustomToastBinding customToastBinding = CustomToastBinding.inflate(LayoutInflater.from(context));
customToastBinding.setMessage(Html.fromHtml(msg).toString());
Toast toast = new Toast(context);
toast.setGravity(Gravity.BOTTOM, 0, 150);
toast.setView(customToastBinding.getRoot());
toast.setDuration(duration);
toast.show();
}
}
public void showToast(View view) {
LayoutInflater inflater = (LayoutInflater)this
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.toast_custom, null);
((TextView)layout.findViewById(android.R.id.message)).setText("Custom Toast");
Toast toast = new Toast(this);
toast.setGravity(Gravity.CENTER, 0, 0);
toast.setDuration(Toast.LENGTH_LONG);
toast.setView(layout);
toast.show();
}
public void show() {
if(context==null) return; // do nothing if context is now null (avoid NullPointerException)
Toast toast = new Toast(context.getApplicationContext());
View view = LayoutInflater.from(context).inflate(R.layout.content_custom_toast, null, false);
TextView custom_text = view.findViewById(R.id.custom_text);
custom_text.setGravity(textGravity);
custom_text.setText(message);
custom_text.setTextSize(textSize);
custom_text.setPadding(padding, padding, padding, padding);
if (!fontFaceString.isEmpty()) {
Typeface typeface = Typeface.createFromAsset(context.getAssets(), fontFaceString);
custom_text.setTypeface(typeface);
}
getShape(type, custom_text);
toast.setDuration(duration == Duration.LONG ? Toast.LENGTH_LONG : Toast.LENGTH_SHORT);
toast.setGravity(gravity, 0, 0);
toast.setView(view);
toast.show();
if(hideToast){
view.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
toast.cancel();
}
});
}
}
/**
* Show the toast for the current application.
*
* @param message the message content
* @param duration the duration
* @param icon the icon resource
* @param gravity the gravity of the toast
*/
public static void showToast(String message, int duration, int icon, int gravity) {
if (message == null || message.equalsIgnoreCase("")) return;
long time = System.currentTimeMillis();
if (!message.equalsIgnoreCase(lastToast) || Math.abs(time - lastToastTime) > 2000) {
View view = LayoutInflater.from(context()).inflate(
R.layout.view_toast, null);
((TextView) view.findViewById(R.id.tv_text)).setText(message);
if (icon != 0) {
ImageView mIcon = (ImageView) view.findViewById(R.id.iv_icon);
mIcon.setImageResource(icon);
mIcon.setVisibility(View.VISIBLE);
}
Toast toast = new Toast(context());
toast.setView(view);
toast.setGravity(gravity, 0, TDevice.getActionBarHeight(context()));
toast.setDuration(duration);
toast.show();
lastToast = message;
lastToastTime = System.currentTimeMillis();
}
}
@SuppressLint({"ResourceAsColor"})
public static void toastInCenter(Context context, String message) {
@SuppressLint("InflateParams") View toastView = LayoutInflater.from(context).inflate(R.layout.toast_center, null);
TextView textView = toastView.findViewById(R.id.tv_toast);
textView.setText(message);
Toast toast = new Toast(context);
toast.setGravity(17, 0, 0);
toast.setDuration(Toast.LENGTH_SHORT);
toast.setView(toastView);
toast.show();
}
/**
* 在应用Application中初始化
*
* @param context 上下文用Context
*/
public void init(Context context) {
View view = LayoutInflater.from(context).inflate(R.layout.view_toast, null);
tvToast = (TextView) view.findViewById(R.id.tv_toast);
//初始化Toast并把View设置给它
toast = new Toast(context);
toast.setView(view);
}
/**
* 显示吐司
*
* @param text 文本
* @param duration 显示时长
*/
private static void show(CharSequence text, int duration) {
cancel();
if (customView != null) {
sToast = new Toast(Utils.getContext());
sToast.setView(customView);
sToast.setDuration(duration);
} else {
sToast = Toast.makeText(Utils.getContext(), text, duration);
}
sToast.setGravity(gravity, xOffset, yOffset);
sToast.show();
}
public void showToast() {
LayoutInflater inflater = getLayoutInflater();
View layout = inflater.inflate(R.layout.custom_toast, null);
Toast toastLocal = new Toast(getApplicationContext());
toastLocal.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
toastLocal.setDuration(Toast.LENGTH_LONG);
toastLocal.setView(layout);
toastLocal.show();
}
public static void show(Context context, CharSequence text, int duration) {
LayoutInflater inflater = ((Activity) context).getLayoutInflater();
View view = inflater.inflate(R.layout.tt_speeker_layout, null);
TextView title = (TextView) view.findViewById(R.id.top_tip);
title.setText(text);
Toast toast = new Toast(context.getApplicationContext());
toast.setGravity(
Gravity.FILL_HORIZONTAL | Gravity.TOP,
0,
(int) context.getResources().getDimension(
R.dimen.top_bar_default_height));
toast.setDuration(duration);
toast.setView(view);
toast.show();
}
public static void showCentorTextToast(Context context, String text) {
if (mToast != null) {
mToast.cancel();
}
View layout = LayoutInflater.from(context).inflate(R.layout.toast_center_text, (ViewGroup) ((Activity) context).findViewById(R.id.toast_layout_root));
((TextView) layout.findViewById(R.id.text)).setText(text);
Toast toast = new Toast(context);
toast.setGravity(16, 0, 0);
toast.setDuration(Toast.LENGTH_LONG);
toast.setView(layout);
toast.show();
}
public CT(Builder builder) {
LayoutInflater inflater = (LayoutInflater) builder.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.custom_layout, null);
TextView tv = layout.findViewById(R.id.cltv);
tv.setText(builder.text);
tv.setTextColor(builder.textCol);
ImageView iv = layout.findViewById(R.id.cliv);
iv.setImageResource(builder.imageRes);
GradientDrawable shape = new GradientDrawable();
shape.setShape(builder.shape);
shape.setCornerRadii(new float[]{
builder.radiusTopLeft,
builder.radiusTopLeft,
builder.radiusTopRight,
builder.radiusTopRight,
builder.radiusBottomRight,
builder.radiusBottomRight,
builder.radiusBottomLeft,
builder.radiusBottomLeft
});
shape.setColor(builder.backCol);
shape.setStroke(builder.borderWidth, builder.borderCol);
layout.setBackgroundDrawable(shape);
Toast toast = new Toast(builder.context);
toast.setView(layout);
toast.setDuration(builder.toastDuration);
toast.setGravity(builder.toastGravity,0,100);
toast.show();
}
private void show(Toast toast, View v, int duration) {
//toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
toast.setDuration(duration);
toast.setView(v);
toast.show();
}
public void showMessage(View view) {
Toast toast = new Toast(this);
toast.setDuration(Toast.LENGTH_LONG);
toast.setView(toastLayout);
toast.show();
}
private void startVoiceTimer() {
Log.d(TAG, "startVoiceTimer: ");
final int countdown = getArguments().getInt(EXTRA_TIMER_DURATION_SECONDS);
mTimerCountdownToast = new Toast(getActivity().getApplicationContext());
mTimerCountdownToast.setGravity(Gravity.CENTER, 0, 0);
mTimerCountdownToast.setDuration(Toast.LENGTH_SHORT);
LayoutInflater inflater = getActivity().getLayoutInflater();
View layout = inflater.inflate(R.layout.toast_timer,
(ViewGroup) getActivity().findViewById(R.id.toast_layout_root));
mTimerCountdownToast.setView(layout);
final TextView label = (TextView) layout.findViewById(R.id.countdown_text);
Timer timer = new Timer("camera_timer");
timer.scheduleAtFixedRate(new TimerTask() {
private int mCountdown = countdown;
@Override
public void run() {
getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
if (mCountdown < 0) {
Log.e(TAG, "Take photo: " + mCountdown);
mTimerCountdownToast.cancel();
takePicture();
} else {
Log.e(TAG, "Execute timer: " + mCountdown);
label.setText(String.format("Photo in %d", mCountdown));
mTimerCountdownToast.show();
}
}
});
mCountdown--;
if (mCountdown < 0) {
cancel();
}
}
}, 1000, 1000);
}
public static Toast makeText(Context context, String message, int duration, int type, boolean androidIcon) {
Toast toast = new Toast(context);
toast.setDuration(duration);
View layout = LayoutInflater.from(context).inflate(R.layout.fancytoast_layout, null, false);
TextView l1 = (TextView) layout.findViewById(R.id.toast_text);
LinearLayout linearLayout = (LinearLayout) layout.findViewById(R.id.toast_type);
ImageView img = (ImageView) layout.findViewById(R.id.toast_icon);
ImageView img1 = (ImageView) layout.findViewById(R.id.imageView4);
l1.setText(message);
if (androidIcon == true)
img1.setVisibility(View.VISIBLE);
else if (androidIcon == false)
img1.setVisibility(View.GONE);
switch (type) {
case 1:
linearLayout.setBackgroundResource(R.drawable.success_shape);
img.setImageResource(R.drawable.ic_check_black_24dp);
break;
case 2:
linearLayout.setBackgroundResource(R.drawable.warning_shape);
img.setImageResource(R.drawable.ic_pan_tool_black_24dp);
break;
case 3:
linearLayout.setBackgroundResource(R.drawable.error_shape);
img.setImageResource(R.drawable.ic_clear_black_24dp);
break;
case 4:
linearLayout.setBackgroundResource(R.drawable.info_shape);
img.setImageResource(R.drawable.ic_info_outline_black_24dp);
break;
case 5:
linearLayout.setBackgroundResource(R.drawable.default_shape);
img.setVisibility(View.GONE);
break;
case 6:
linearLayout.setBackgroundResource(R.drawable.confusing_shape);
img.setImageResource(R.drawable.ic_refresh_black_24dp);
break;
}
toast.setView(layout);
return toast;
}
/**
* show toast
* @author leibing
* @createTime 2017/5/25
* @lastModify 2017/5/25
* @param context
* @param view
* @return
*/
public static void show(Context context, View view) {
Toast toast = new Toast(context);
toast.setGravity(Gravity.CENTER, 0, 0);
toast.setView(view);
toast.show();
}