Android夜间模式的几种实现

2019-05-18 12:40:35

参考文章 Android夜间模式的几种实现  我更倾向于第三种,简单直接,快速



一、直接修改widget颜色,这种方式实现起来最简单,但需要每个控件都去修改,太过复杂。例如:

复制代码

    /**
     * 相应交互,修改控件颜色
     * @param view     */
    public void onMethod1Click(View view) {        if (view.getId() == R.id.btn_method1) {            int theme = NightModeUtils.getSwitchDayNightMode(this);
            NightModeUtils.setBackGroundColor(this, mRootView, theme);
            NightModeUtils.setTextColor(this, findViewById(R.id.text), theme);
            NightModeUtils.setDayNightMode(this, theme);
        }
    }

复制代码

NightModeUitls修改颜色方法

复制代码

    /**
     * 修改背景色
     * @param context
     * @param view
     * @param theme     */
    public static void setBackGroundColor(Context context, View view, int theme) {        int color = context.getResources().getColor(
                theme == THEME_SUN ? R.color.light_color : R.color.night_color);
        view.setBackgroundColor(color);
    }    /**
     * 修改文字色
     * @param context
     * @param view
     * @param theme     */
    public static void setTextColor(Context context, View view, int theme) {        int color = context.getResources().getColor(
                theme == THEME_SUN ? R.color.night_color : R.color.light_color);
        TextView textView = (TextView)view;
        textView.setTextColor(color);
    }

复制代码

二、通过修改Theme,更新应用主题。这种方法问题在于,需要重启Activity才能完成界面渲染。

在Activity中调用setContentView之前进行Theme设置:

    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);
        NightModeUtils.onActivityCreateSetTheme(this);
        setContentView(R.layout.activity_main);
    }

 

NightModeUitls设置Theme方法:

复制代码

    /** Set the theme of the activity, according to the configuration. */
    public static void onActivityCreateSetTheme(Activity activity) {        int theme = getDayNightMode(activity);        switch (theme) {            case THEME_SUN:
                activity.setTheme(R.style.AppSunTheme);                break;            case THEME_NIGHT:
                activity.setTheme(R.style.AppNightTheme);                break;            default:                break;
        }
    }

复制代码

 

三、通过怎加一层遮光罩来实现。效果不是很理想。

通过WindowManager,将一个透明背景的TextView加到Activity主界面中。代码如下:

复制代码

    private void night() {        if (mNightView == null) {
            mNightView = new TextView(this);
            mNightView.setBackgroundColor(0xaa000000);
        }

        WindowManager.LayoutParams lp = new WindowManager.LayoutParams(
                LayoutParams.MATCH_PARENT,
                LayoutParams.MATCH_PARENT,
                WindowManager.LayoutParams.TYPE_APPLICATION,
                WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
                PixelFormat.TRANSLUCENT);
        lp.gravity = Gravity.BOTTOM;
        lp.y = 10;        try {
            mWindowManager.addView(mNightView, lp);
        } catch (Exception ex) {
        }
    }    private void day() {        try {
            mWindowManager.removeView(mNightView);
        } catch (Exception ex) {
        }
    }

复制代码

四、最后来看一下Dialog需要怎么实现夜间模式。

AlertDialog.Builder 有一个带style id参数的构造函数,我们就通过这个构造函数来实现Dialog主题的修改,从而达到夜间模式。

复制代码

    public static AlertDialog.Builder createBuilder(Context context) {        if (NightModeUtils.getDayNightMode(context) == NightModeUtils.THEME_SUN) {            return new AlertDialog.Builder(context);
        } else {            return new AlertDialog.Builder(context, R.style.NightDialog);
        }
    }

复制代码

我们通过如上方法来获取Builder,实现主题切换。其中R.style.NightDialog我采用如下方式:

    <style name="NightDialog" parent="android:Theme.Holo.Dialog">
        <item name="android:windowBackground">@android:color/transparent</item>
    </style>

在android honeycomb之前的版本Theme.Dialog.Alert与AlertDialog这两个style是public的,可以通过修改主题时,重新定义这两个style实现dialog主题的修改,但之后的版本已经将他们开放关闭了。所以,我通过上面的办法实现了dialog的主题修改。


  • 2018-03-14 17:41:44

    MySQL的if,case语句使用总结

    Mysql的if既可以作为表达式用,也可在存储过程中作为流程控制语句使用,如下是做为表达式使用:

  • 2018-03-16 23:56:14

    layer-list -- layer-list的基本使用介绍

    简单理解,layer 是层,list 是列表,那么 layer-list 就是层列表的意思。但是,是什么层列表呢?? 其实 layer-list 是用来创建 LayerDrawable 的,LayerDrawable 是 DrawableResource 的一种, 所以,layer-list 创建出来的是 图层列表,也就是一个drawable 图形。

  • 2018-03-20 22:01:18

    Java如何进行Base64的编码(Encode)与解码(Decode)?

    Base64是一种能将任意Binary资料用64种字元组合成字串的方法,而这个Binary资料和字串资料彼此之间是可以互相转换的,十分方便。在实际应用上,Base64除了能将Binary资料可视化之外,也常用来表示字串加密过后的内容。如果要使用Java 程式语言来实作Base64的编码与解码功能,可以参考本篇文章的作法。

  • 2018-03-20 23:38:05

    PHP中json_encode与json_decode

    json_encode() 对变量进行JSON编码,json_decode() 对JSON数据进行解码,转换为PHP变量

  • 2018-03-21 22:26:03

    Android BASE64Encoder不能用的问题

    昨天项目与后台交互的接口传参数需要加密,用的是BASE64Encoder加密,可是这个类不能用,谷歌了一下说的是:

  • 2018-03-21 22:28:02

    Java加密算法 AES

    AES 算法 对称加密,密码学中的高级加密标准 2005年成为有效标准