Android开发工具类之Toast
2017-03-30 04:24
302 查看
Toast遇到一次写一次好麻烦,所以弄成一个工具类,调用方便点。
下面是xml代码 想怎么设置就怎么设置
参考了下这个:https://my.oschina.net/banxi/blog/56007
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>
参考了下这个:https://my.oschina.net/banxi/blog/56007
相关文章推荐
- Android开发中4个常用的工具类【Toast、SharedPreferences、网络及屏幕操作】
- 【安卓开发】可爱的android开发者们,给跳出小提示窗控件取名为Toast(吐司面包)的详解
- Android开发学习笔记:浅谈Toast
- Android开发实现的IntentUtil跳转多功能工具类【包含视频、音频、图片、摄像头等操作功能】
- Android开发--身高体重指数(BIM)计算--添加对话框(Dialog)(Toast界面组件、错误处理try-catch)
- Android 开发之 Toast (弹出提示框)
- Android 开发 带图像的Toast提示
- android应用开发笔记---常用控件RadioGroup和RadioButton及Toast
- Android开发_弹出小小提示框_Toast
- Android软件开发之 自定义别样Toast
- Android开发--wifi工具类资料
- Android腾讯微薄客户端开发七:图片加圆角以及时间处理工具类
- Android上面开发自己的Toast系统的思路
- Android开发实现HttpClient工具类
- 【Android 开发教程】Toast通知
- Android控件开发之Toast
- Android开发实现HttpClient工具类
- Android开发技巧:永不关闭的Toast信息框
- Android开发 --- Toast