Android 自定义带图标Toast,工具方法,Toast自定义显示时间
生活随笔
收集整理的這篇文章主要介紹了
Android 自定义带图标Toast,工具方法,Toast自定义显示时间
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
?帶圖標Toast工具方法1
樣式
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/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="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/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 2020/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="http://schemas.android.com/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="http://schemas.android.com/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@163.com* 日期:2018/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();} }?
總結
以上是生活随笔為你收集整理的Android 自定义带图标Toast,工具方法,Toast自定义显示时间的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 如何转换html转化mp4,格式互转技巧
- 下一篇: Maven的安装以及在Myeclipse