Android夜间模式的几种实现

摘要:
代码如下:privatenoidnight(){if{mNightView=newTextView;mNightView.setBackgroundColor;}WindowManager.LayoutParamslp=newWindowManager。LayoutParams;lp.egravity=重力。底部;低压。y=10;尝试{mWindowManager.addView;}catch{}}privatevoiday(){try{mWindowManager.removeView;}捕捉{}}IV。最后,让我们看看Dialog需要如何实现夜间模式。警报对话框。Builder有一个带有styleid参数的构造函数,通过它我们可以修改Dialog主题以实现夜间模式。publicstaticAlertDialog.BuildercreateBuilder{if{returnnewAlertDialog.Builder;}否则{returnnewAlertDialog.Builder;}}我们使用上述方法来获取生成器并实现主题切换。其中,R.style。NightDialog采用以下方法:@android:color/transparent主题。对话框,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的主题修改。

Demo源码

免责声明:文章转载自《Android夜间模式的几种实现》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇如何在交互式环境中执行Python程序MySQL 修改最大连接数(max_connections)失效,上限214问题下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

Canvas:绘制路径

Canvas:绘制路径 绘制路径   图形的基本元素是路径。路径是[通过不同颜色和宽度的线段或曲线相连形成的不同形状的]点的集合。一个路径,甚至一个子路径,都是闭合的。   使用路径绘制图形需要一些额外的步骤。 首先,你需要创建路径起始点。 然后你使用画图命令去绘制路径。 之后把路径进行封闭。 一旦路径生成,你就能通过描边或填充路径区域来渲染图形。 函...

SpringMVC加载配置Properties文件的几种方式

最近开发的项目使用了SpringMVC的框架,用下来感觉SpringMVC的代码实现的非常优雅,功能也非常强大, 网上介绍Controller参数绑定、URL映射的文章都很多了,写这篇博客主要总结一下SpringMVC加载配置Properties文件的几种方式 1.通过context:property-placeholde实现配置文件加载   1.1、在...

Android网络连接判断与处理

获取网络信息需要在AndroidManifest.xml文件中加入相应的权限。 <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> 1)判断是否有网络连接 publicboolean isNetworkConnected(Context cont...

基于Abp VNext框架设计

abp 通过IDistributedEventBus接口集成自IEventBus实现分布式事件消息的发布订阅。 IEventBus在什么时机触发PublishAsync? 当前UnitOfWork完成时,触发IEventBus的PublishAsync 在没有事务环境下,同步调用IEventBus的PublishAsync abp 默认实现基于Ra...

ActivityManagerService的启动过程

AMS对象随系统进程启动而构建,随着系统进程退出而消亡,可以说,AMS与系统进程共存亡。 先上一张总的启动时序图: 上图分为三个步骤: 初始化系统进程的运行环境; 初始化AMS对象; AMS对象启动的配套工作。 1.初始化系统进程的运行环境 SystemServer是我们理解Android系统进程的入口,它的初始化是从Native层开始的:Zygot...

odoo开发笔记:Server+Action服务器动作自动触发执行

       Odoo的市场定位是SME(中小型企业),这个市场的ERP产品,多如牛毛,产品各具特色。不过,Odoo的自动化处理机制,可以睥睨天下,无人能及。包括一些大型国产软件,如用友、金蝶也不具备如此强大的自动化业务处理功能。Odoo的业务自动化机制,可以非常容易地扩充ERP系统功能,非常容易地让业务工作自动化。 Odoo自动化动作 如下图,增...