解决android7.0+ 自定义Toast时长,但是toast不显示问题

重点在于解决部分机型自定义Toast 时长不显示问题
在部分手机上会出现自定义时长的Toast不显示,从而导致出现bug(排除Toast权限被关闭情况),这样我们需要自定义Toast 代码如下

public class ToastUtils {
    private static final String TAG = ToastUtils.class.getSimpleName();
    private static TextView tv;
    public static final int LENGTH_LONG = 3500; // 3.5 seconds
    public static final int LENGTH_SHORT = 2000; // 2 seconds

    private static View mNextView;
    private static int mGravity, mX, mY;
    private static final WindowManager.LayoutParams mParams = new WindowManager.LayoutParams();
    private static WindowManager mWM;
    private static Handler mHanlder = new Handler();

    /**
     * init
     * @param context
     */
    @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
    private static void init(Context context) {
        mY = context.getResources().getDimensionPixelSize(
                R.dimen.toast_y_offset);
        mGravity = context.getResources().getInteger(
                R.integer.config_toastDefaultGravity);
        LayoutInflater inflate = (LayoutInflater)
                context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        mNextView = inflate.inflate(R.layout.transient_notification, null);
        TextView tv = (TextView) mNextView.findViewById(android.R.id.message);

        mParams.height = WindowManager.LayoutParams.WRAP_CONTENT;
        mParams.width = WindowManager.LayoutParams.WRAP_CONTENT;
        mParams.format = PixelFormat.TRANSLUCENT;
        mParams.windowAnimations = R.style.Animation_Toast;
        mParams.type = WindowManager.LayoutParams.TYPE_TOAST;
        mParams.setTitle("Toast");
        mParams.flags = WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON
                | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
                | WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE;


        mWM = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        // We can resolve the Gravity here by using the Locale for getting
        // the layout direction
        final Configuration config = mNextView.getContext().getResources().getConfiguration();
        final int gravity = Gravity.getAbsoluteGravity(mGravity, config.getLayoutDirection());
        mParams.gravity = gravity;
        if ((gravity & Gravity.HORIZONTAL_GRAVITY_MASK) == Gravity.FILL_HORIZONTAL) {
            mParams.horizontalWeight = 1.0f;
        }
        if ((gravity & Gravity.VERTICAL_GRAVITY_MASK) == Gravity.FILL_VERTICAL) {
            mParams.verticalWeight = 1.0f;
        }
        mParams.x = mX;
        mParams.y = mY;
//        mParams.verticalMargin = mVerticalMargin;
//        mParams.horizontalMargin = mHorizontalMargin;
        mParams.packageName = context.getPackageName();
    }

    /**
     * Show the view for the specified duration.
     * @param context
     * @param text
     * @param duration
     */
    public static void show(final Context context, final CharSequence text, int duration) {
        if (context == null) {
            throw new RuntimeException("context is null");
        }

        if (mWM == null || mNextView == null) {
            init(context);
        }
        mHanlder.removeCallbacks(cancelRunable);
        mHanlder.post(new Runnable() {
            @Override
            public void run() {
                ((TextView) mNextView.findViewById(android.R.id.message)).setText(text);
                if (mNextView.getParent() != null)
                    mWM.removeView(mNextView);
                mWM.addView(mNextView, mParams);
            }
        });
        mHanlder.postDelayed(cancelRunable, duration);
    }

    private static Runnable cancelRunable = new Runnable() {
        @Override
        public void run() {
            cancel();
        }
    };

    /**
     * cancel toast
     */
    public static void cancel() {
        if (mNextView != null && mNextView.getParent() != null)
            mWM.removeViewImmediate(mNextView);
    }
}

布局文件如下

<?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="wrap_content"
   android:paddingLeft="@dimen/dimens_15_dp"
   android:paddingRight="@dimen/dimens_15_dp"
   android:orientation="vertical">

   <TextView
       android:id="@+id/tv_toast_name"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:background="@drawable/shape_toast"
       android:gravity="center"
       android:layout_gravity="right"
       android:paddingBottom="10dp"
       android:paddingLeft="30dp"
       android:paddingRight="30dp"
       android:paddingTop="10dp"
       android:text=""
       android:textColor="#FFFFFF"
       android:textSize="12sp" />

</LinearLayout>

这中间需要注意的 不能造成窗口泄漏以及内存泄漏(主要是 Context泄漏)。其他需求可以自己修改。

你可能感兴趣的:(Android)