100字范文,内容丰富有趣,生活中的好帮手!
100字范文 > Android 自定义带图标Toast 工具方法 Toast自定义显示时间

Android 自定义带图标Toast 工具方法 Toast自定义显示时间

时间:2024-05-27 14:44:47

相关推荐

Android 自定义带图标Toast 工具方法 Toast自定义显示时间

带图标Toast工具方法1

样式

<?xml version="1.0" encoding="utf-8"?><shape xmlns:android="/apk/res/android"><corners android:radius="10dp" /><solid android:color="@color/color_a000" /></shape>

<!--toast半透明色--><color name="color_a000">#aa000000</color>

文件

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="/apk/res/android"xmlns:tools="/tools"android:layout_width="wrap_content"android:layout_height="wrap_content"android:background="@drawable/shape_bg_a000_r10"android:gravity="center"android:orientation="vertical"><ImageViewandroid:id="@+id/iamge"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginLeft="60dp"android:layout_marginRight="60dp"android:layout_marginTop="34dp" /><TextViewandroid:id="@+id/tv_content"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_gravity="center_horizontal"android:layout_marginBottom="34dp"android:layout_marginTop="20dp"android:gravity="center"android:textColor="@color/white"android:textSize="30sp" /></LinearLayout>

工具类

/*** Created by meixi on /11/12.*/public class ToastUtil {private static Toast toast;public static void showToastCenter(Context ctx, String str) {Toast toast = Toast.makeText(ctx, str, Toast.LENGTH_SHORT);toast.setGravity(0, Gravity.CENTER, Gravity.CENTER);toast.show();}public static void showToastLong(Context ctx, String str) {Toast.makeText(ctx, str, Toast.LENGTH_LONG).show();}public static void showToastShort(Context ctx, String str) {Toast.makeText(ctx, str, Toast.LENGTH_SHORT).show();}public static void showToast(Activity activity, String toastContent, int image,int time) {LayoutInflater inflater = activity.getLayoutInflater();View layout = inflater.inflate(R.layout.toast_stat_layout, null);TextView content = layout.findViewById(R.id.tv_content);content.setText(toastContent);ImageView imageView = layout.findViewById(R.id.iamge);imageView.setImageResource(image);if (toast==null){toast = new Toast(activity);}toast.setGravity(Gravity.CENTER, 0, 0);toast.setDuration(Toast.LENGTH_LONG);toast.setView(layout);// toast.show();showMyToast(toast,time);}public static void showMyToast(final Toast toast, final int cnt) {final Timer timer =new Timer();timer.schedule(new TimerTask() {@Overridepublic void run() {LogPlus.e("lgq","收尾。。show。。");toast.show();}},0,3000);new Timer().schedule(new TimerTask() {@Overridepublic void run() {toast.cancel();timer.cancel();}}, cnt );}}

调用

//自定义显示时间

ToastUtil.showToast(MainActivity.this, "失败", R.mipmap.ic_failure,30000);

简易Toast工具方法2

private void displayToastTip(final String tip) {

runOnUiThread(new Runnable() {

@Override

public void run() {

Toast.makeText(SfzShiBieActivity.this, tip, Toast.LENGTH_SHORT).show();

}

});

}

private void toast(final String text) {

handler.post(new Runnable() {

@Override

public void run() {

Toast.makeText(RegActivity.this, text, Toast.LENGTH_LONG).show();

}

});

}

private Handler handler = new Handler(Looper.getMainLooper());

背景资源

1、<color name="numtext">#999999</color>

2、

<?xml version="1.0" encoding="utf-8"?><shape xmlns:android="/apk/res/android"><!-- 设置透明背景色 --><solid android:color="@color/numtext" /><!-- 设置一个黑色边框 --><strokeandroid:width="2px"android:color="@color/numtext" /><!-- 设置四个圆角的半径 --><cornersandroid:radius="8dp"/><!-- 设置一下边距,让空间大一点 --><paddingandroid:bottom="7dp"android:left="9dp"android:right="9dp"android:top="7dp" /></shape>

3、layout.xml布局

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"><!-- android:minHeight="80dp"--><LinearLayoutandroid:layout_width="120dp"android:layout_height="80dp"android:layout_gravity="center"android:background="@drawable/backtoa"android:baselineAligned="false"android:gravity="center"android:orientation="vertical"android:padding="5dp"><!--android:background="@drawable/toast_bg"--><ImageViewandroid:id="@+id/toast_image"android:layout_width="30dp"android:layout_height="30dp"android:layout_gravity="center"android:layout_margin="2dp"android:background="@mipmap/em_dx_checkbox_on" /><TextViewandroid:id="@+id/toast_text"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_gravity="center"android:layout_margin="2dp"android:text="保存成功"android:textColor="#ffffff"android:textSize="15dp" /></LinearLayout></LinearLayout>

4、工具类

/*** 作者:created by meixi* 邮箱:13164716840@* 日期:/8/28 08*/public class ToastUtils {private static Context mContext ;public static void showToast(String toast,Context context) {mContext = context;Toast.makeText(mContext, toast, Toast.LENGTH_SHORT).show();}/*** 带图片的吐司提示* @param text*/public static void showCustomImgToast(String text ,Context context) {mContext = context;LayoutInflater inflater = LayoutInflater.from(mContext);View view = inflater.inflate(R.layout.toast_view, null);ImageView imageView = (ImageView) view.findViewById(R.id.toast_image);imageView.setBackgroundResource(R.mipmap.em_dx_checkbox_on);TextView t = (TextView) view.findViewById(R.id.toast_text);t.setText(text);Toast toast = null;if (toast != null) {toast.cancel();}toast = new Toast(mContext);toast.setDuration(Toast.LENGTH_SHORT);toast.setView(view);toast.show();}/*** 带图片的吐司提示* 通过参数传递,可是设置吐司的图片和文字内容* @param text*/public static void showCustomImgToast(String text,int imgResId) {LayoutInflater inflater = LayoutInflater.from(mContext);View view = inflater.inflate(R.layout.toast_view, null);ImageView imageView = (ImageView) view.findViewById(R.id.toast_image);imageView.setBackgroundResource(R.mipmap.em_dx_checkbox_on);TextView t = (TextView) view.findViewById(R.id.toast_text);t.setText(text);Toast toast = null;if (toast != null) {toast.cancel();}toast = new Toast(mContext);toast.setDuration(Toast.LENGTH_SHORT);toast.setView(view);toast.show();}/*** 不带图片的吐司提示* @param text*/public static void showCustomToast(String text) {LayoutInflater inflater = LayoutInflater.from(mContext);View view = inflater.inflate(R.layout.toast_view, null);ImageView imageView = (ImageView) view.findViewById(R.id.toast_image);imageView.setVisibility(View.GONE);TextView t = (TextView) view.findViewById(R.id.toast_text);t.setText(text);Toast toast = null;if (toast != null) {toast.cancel();}toast = new Toast(mContext);toast.setDuration(Toast.LENGTH_SHORT);toast.setView(view);toast.show();}/*** 带图片的吐司,设置吐司弹出的位置为屏幕中心* @param text*/public static void showCustomToastCenter(String text ,Context context) {showCustomToastCenter(text, R.mipmap.em_dx_checkbox_on,context);}/*** 带图片的吐司,设置吐司弹出的位置为屏幕中心* 通过参数传递,可是设置吐司的图片和文字内容* @param text*/public static void showCustomToastCenter(String text, int imgResId,Context context) {mContext = context;LayoutInflater inflater = LayoutInflater.from(mContext);View view = inflater.inflate(R.layout.toast_view, null);ImageView imageView = (ImageView) view.findViewById(R.id.toast_image);imageView.setBackgroundResource(imgResId);TextView t = (TextView) view.findViewById(R.id.toast_text);t.setText(text);Toast toast = null;if (toast != null) {toast.cancel();}toast = new Toast(mContext);toast.setDuration(Toast.LENGTH_SHORT);toast.setView(view);toast.setGravity(Gravity.BOTTOM, 0, 50);toast.show();}}

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。