导航号,我的单页导航
文章目录

前言

前面介绍过Android静态换肤日夜模式的切换(原文:《Android静态换肤-日夜主题切换之继承Activity记录 》),从中我们知道,ToastDialog存在无法切换成功。原因是Toast或者Dialog是需要时才去初始化的,此时换肤插件没法获取到View。

PS:日夜模式不存在这个问题,当时记错了,应该动态换肤才有这个问题。因为Android本身就对日夜模式进行了优化,主动会重新获取对应模式下的资源。

本文就不删除了,作为自己的过程。

解决这个问题其实有两种方式

  1. 弹出Toast或Dialog时显示前通过换肤插件SkinManager获取当前皮肤的背景和颜色资源
  2. 提前初始化Toast和Dialog的布局即可

第一个方式就是我们在需要时重新设置;对于第二种方式,就是提前加载布局,让换肤lib获取到对应的View,这样就会自动切换。

今天就简单记录一下第二种,方便自己查阅。

正文

直入正题。

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    //Toast和Dialog提前初始化Layout
    ToastUtils.getInstance().initLayout(SkinApp.getContext());
    DialogUtils.getInstance().initLayout(SkinApp.getContext());
}

Toast

也就是Toast的简单封装。

Toast.java
public class ToastUtils {
    private Toast mToast = null;
    private View mRootView = null;
    private Context mContext;

    private ToastUtils() {
    }

    private static class ToastHolder {
        @SuppressLint("StaticFieldLeak")
        static ToastUtils toastUtils = new ToastUtils();
    }

    public static ToastUtils getInstance() {
        return ToastHolder.toastUtils;
    }

    @SuppressLint("InflateParams")
    public void initLayout(Context context) {
        mContext = context;
        mRootView = LayoutInflater.from(context).inflate(R.layout.toast_tips_layout, null);
    }

    public void show(int gravity, int xOffset, int yOffset, int duration, String message) {
        if (null == mContext || null == mRootView) {
            throw new RuntimeException("Please call initLayout() first.");
        }
        hide(); //确保只有一个
        mToast = new Toast(mContext);
        mToast.setView(mRootView);
        mToast.setGravity(gravity, xOffset, yOffset);
        ((TextView) mRootView.findViewById(R.id.toast_tv_tip_text)).setText(message);
        mToast.setDuration(duration);
        mToast.show();
    }

    public void hide() {
        if (null != mToast) {
            mToast.cancel();
            mToast = null;
        }
    }

    public void release() {
        hide();
        mRootView = null;
        mContext = null;
    }
}

Dialog

跟Toast差不多

styles.xml
<style name="dialog_theme" parent="@android:style/Theme.Dialog">
    <item name="android:windowFrame">@null</item>
    <item name="android:windowIsFloating">true</item>
    <item name="android:windowIsTranslucent">true</item>
    <item name="android:windowNoTitle">true</item>
    <item name="android:background">@android:color/transparent</item>
    <item name="android:windowBackground">@android:color/transparent</item>
    <item name="android:backgroundDimEnabled">true</item>
    <item name="android:backgroundDimAmount">0.5</item>
</style>
DialogUtils.java
public class DialogUtils {

    private static Dialog mDialog = null;
    private View mRootView = null;

    private DialogUtils() {
    }

    private static class DialogHolder {
        @SuppressLint("StaticFieldLeak")
        static DialogUtils dialogUtils = new DialogUtils();
    }

    public static DialogUtils getInstance() {
        return DialogHolder.dialogUtils;
    }

    @SuppressLint("InflateParams")
    public void initLayout(Context context) {
        mRootView = LayoutInflater.from(context).inflate(R.layout.dialog_tips_layout, null);
        mDialog = new Dialog(context, R.style.dialog_theme);
    }

    public void show(int gravity, int width, int height, boolean cancel, String message) {
        if (null == mDialog || null == mRootView) {
            throw new RuntimeException("Please call initLayout() first.");
        }
        ((TextView) mRootView.findViewById(R.id.dialog_tv_tip_text)).setText(message);
        RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(width, height);
        mDialog.setContentView(mRootView, layoutParams);
        mDialog.setCanceledOnTouchOutside(cancel);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            mDialog.getWindow().setType(WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY);
        } else {
            mDialog.getWindow().setType(WindowManager.LayoutParams.TYPE_PHONE);
        }

        mDialog.getWindow().setGravity(gravity);
        mDialog.show();
    }

    public void hide() {
        if (null != mDialog) {
            mDialog.dismiss();
        }
    }

    public boolean isShowing() {
        if (null != mDialog) {
            return mDialog.isShowing();
        }
        return false;
    }

    public void release() {
        hide();
        mDialog = null;
        mRootView = null;
    }
}

参考文章

© 版权声明
导航号,我的单页导航

暂无评论

暂无评论...