Android 自定义Toast
来源:互联网 发布:西装裤配什么鞋子知乎 编辑:程序博客网 时间:2024/06/10 11:04
自定义Toast
概述:
系统的toast有时候不能满足我们的要求,这时候我们就需要自定义Toast样式来满足我的需求,比如增大字体、加个图标等等……
效果图
- 自定义Toast 继承android.widget.Toast
/** * Created by TangRen on 2016/7/18. */public class Toast extends android.widget.Toast { /** * Construct an empty Toast object. You must call {@link #setView} before you * can call {@link #show}. * * @param context The context to use. Usually your {@link Application} * or {@link Activity} object. */ private static LayoutInflater inflater; public Toast(Context context) { super(context); } /** * 自定义的Toast * * @param context * @param text * @param duration 消失的时间 * @return */ public static Toast makeText(Context context, CharSequence text, int duration) { Toast toast = new Toast(context); inflater = inflater.from(context); View view = inflater.inflate(R.layout.toast, null); TextView textView = (TextView) view.findViewById(R.id.toast_text); textView.setText(text); toast.setView(view); toast.setGravity(Gravity.BOTTOM, 0, formatDipToPx(context, 80)); toast.setDuration(duration); return toast; } /** * 把dip单位转成px单位 * * @param context * @param dip * @return */ public static int formatDipToPx(Context context, int dip) { DisplayMetrics dm = new DisplayMetrics(); ((Activity) context).getWindowManager().getDefaultDisplay() .getMetrics(dm); return (int) Math.ceil(dip * dm.density); } /** * 把px单位转成dip单位 * * @param context * @param px * @return */ public static int formatPxToDip(Context context, int px) { DisplayMetrics dm = new DisplayMetrics(); ((Activity) context).getWindowManager().getDefaultDisplay() .getMetrics(dm); return (int) Math.ceil(((px * 160) / dm.densityDpi)); }}
2.toast.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:layout_marginLeft="20dp" android:layout_marginRight="20dp" android:background="@drawable/toast_shape" android:gravity="center" android:orientation="horizontal"> <TextView android:id="@+id/toast_text" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_marginBottom="20dp" android:layout_marginLeft="10dp" android:layout_marginRight="10dp" android:layout_marginTop="20dp" android:layout_weight="1" android:gravity="center" android:text="小兵智能科技欢迎您!" android:textColor="#FFFFFF" android:textSize="17dp" /></LinearLayout>
3.样式文件toast_shape.xml
<?xml version="1.0" encoding="utf-8"?><shape xmlns:android="http://schemas.android.com/apk/res/android"> <corners android:radius="5dp"></corners> <solid android:color="#aa000000"></solid> <padding android:bottom="5dp" android:left="5dp" android:right="5dp" android:top="5dp" /></shape>
3.使用
findViewById(R.id.button).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Toast.makeText(MainActivity.this,"Hello world!",Toast.LENGTH_LONG).show(); } });
华丽丽的分割线
其实按照上面写一般情况下是可以处理大部分场景,但是当用户连续点击的时候就会出现连续Toast信息,当然人的肉眼是无法观察到的,那么这个时候该如何处理呢?
package com.szxb.font;import com.example.toastdemo.R;import android.annotation.SuppressLint;import android.content.Context;import android.os.Handler;import android.view.Gravity;import android.view.LayoutInflater;import android.view.View;import android.widget.TextView;public class Toast extends android.widget.Toast { /** * @author TangRen * @param args * @time 2016-7-28 */ private static Toast toast; private static Handler handler = new Handler(); private static Runnable runnable = new Runnable() { public void run() { toast.cancel(); toast = null; } }; public Toast(Context context) { super(context); } @SuppressLint("InflateParams") public static Toast makeToast(Context context, CharSequence text) { LayoutInflater inflater = (LayoutInflater) context .getSystemService(Context.LAYOUT_INFLATER_SERVICE); View view = inflater.inflate(R.layout.toast, null); TextView textView = (TextView) view.findViewById(R.id.toast_text); textView.setText(text); handler.removeCallbacks(runnable); if (toast == null) { toast = new Toast(context); toast.setView(view); toast.setGravity(Gravity.BOTTOM, 0, PixelFormat.formatDipToPx(context, 70)); toast.setDuration(Toast.LENGTH_SHORT); } handler.postDelayed(runnable, 1500);//显示1.5S消失 toast.show(); return toast; }}
Toast.makeToast(MainActivity.this, "小孩子能够").show();
0 0
- android自定义Toast视图
- Android中自定义Toast
- Android自定义Toast样式
- android Toast 自定义样式
- 自定义Toast - Android代码
- android中自定义toast
- Android 自定义Toast
- android自定义Toast视图
- android 自定义toast
- 自定义Android Toast
- android自定义toast
- Android Toast自定义
- android自定义toast
- Android之自定义Toast
- Android 自定义Toast实现
- android 自定义toast
- Android Toast自定义
- android 自定义toast
- Ehcache使用
- Android Studio修改项目编译版本
- [leetcode] 27. Remove Element
- Android AllView使用效果
- GP学习(七)—Accessing raster workspaces
- Android 自定义Toast
- JS模块化
- 通达OA列表控件添加js代码,实现js的获取和赋值
- Java JVM:内存溢出(栈溢出,堆溢出,持久代溢出以及 nable to create native thread)
- tips.js提示框插件
- 【杭电】[1865]1sting
- 242. Valid Anagram
- SQLSERVER 返回错误代码并显示出来
- NODE.JS的简单的例子