Android 夜间模式的实现

package com.loaderman.daynightdemo;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.app.AppCompatDelegate;
import android.support.v7.widget.Toolbar;
import android.view.View;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    {
        AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_AUTO);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        findViewById(R.id.btn_auto).setOnClickListener(this);
        findViewById(R.id.btn_day).setOnClickListener(this);
        findViewById(R.id.btn_night).setOnClickListener(this);
        findViewById(R.id.fab).setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_auto: {
                getDelegate().setLocalNightMode(AppCompatDelegate.MODE_NIGHT_AUTO);
                recreate();
                break;
            }
            case R.id.btn_day: {
                getDelegate().setLocalNightMode(AppCompatDelegate.MODE_NIGHT_NO);
                recreate();
                break;
            }
            case R.id.btn_night: {
                getDelegate().setLocalNightMode(AppCompatDelegate.MODE_NIGHT_YES);
                recreate();
                break;
            }
        }
    }
}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context="com.yanzhenjie.daynight.MainActivity">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:popupTheme="@style/AppTheme.PopupOverlay"/>

    </android.support.design.widget.AppBarLayout>

    <include layout="@layout/content_main"/>

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|end"
        android:layout_margin="@dimen/fab_margin"
        android:src="@android:drawable/ic_dialog_email"/>

</android.support.design.widget.CoordinatorLayout>

content_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context="com.yanzhenjie.daynight.MainActivity"
    tools:showIn="@layout/activity_main">

    <Button
        android:id="@+id/btn_auto"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="自动模式" />

    <Button
        android:id="@+id/btn_day"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="日间模式" />

    <Button
        android:id="@+id/btn_night"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="夜间模式" />
</LinearLayout>

style.xml

<resources>

    <style name="AppTheme" parent="Theme.AppCompat.DayNight.DarkActionBar">
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
    </style>

    <style name="AppTheme.NoActionBar">
        <item name="windowActionBar">false</item>
        <item name="windowNoTitle">true</item>
    </style>

    <style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />

    <style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />

</resources>

AndroidMainfest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.loaderman.daynightdemo">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name"
            android:theme="@style/AppTheme.NoActionBar">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

效果图:

时间: 2024-08-05 07:29:57

Android 夜间模式的实现的相关文章

Android夜间模式实现

查看我的全部开源项目[开源实验室] 欢迎加入我的QQ群:[201055521],本博客客户端下载[请点击] 本文原创,转载请注明地址:http://blog.kymjs.com/ 最近在做一个Android夜间模式的功能,又重新研究了一下Theme机制.贯彻我的风格,以解决问题为目标,写了个小Demo,简单了实现了切换夜间模式,为大家讲解. 在Activity中有一个方法叫setTheme(),可以设置Activity的Theme,当然Application类中也有相同的方法,也可以在Appli

Android 高级UI设计笔记24:Android 夜间模式之 WebView 实现白天 / 夜间阅读模式 (使用JavaScript)

1. 问题引入: 前面我们是使用方法 降低屏幕亮度(不常用) 和 替换theme,两者都是针对Activity的背景进行白天.夜间模式的交换,但是如果我们显示的是Html的内容,这个时候改怎么办? 分析:首先加载Html页面肯定是要用到WebView的,通过loadUrl的方法可以把html页面显示到webView,我们知道Android可以与JavaScript进行交互,就是说可以在JavaScript中调用Android的代码,也可以在Android中调用JavaScript代码. 所以就有

Android 高级UI设计笔记23:Android 夜间模式之 两种常用方法(降低屏幕亮度+替换theme)

1. 夜间模式 所谓的夜间模式,就是能够根据不同的设定,呈现不同风格的界面给用户,而且晚上看着不伤眼睛.特别是一些新闻类App实现夜间模式是非常人性化的,增强用户体验. 2. 我根据网上的资料 以及自己代码亲测,总结如下两种方法: (1)降低屏幕亮度 (2)替换theme 3. 夜间模式之 降低屏幕亮度: (1)创建一个Android工程,命名为"夜间模式_利用屏幕亮度(App)",如下: (2)首先我们来到主布局之中,如下: 1 <LinearLayout xmlns:andr

Android应用开发中的夜间模式实现(一)

前言 在应用开发中会经常遇到要求实现夜间模式或者主题切换具体例子如下,我会先讲解第一种方法. 夜间模式 知乎 网易新闻 沪江开心词场 Pocket 主题切换 腾讯QQ 新浪微博 我今天主要是详述第一种的实现方式: 首先,应用的Application要继承自定义的Theme 1 2 3 4 5 6 <application android:allowBackup="true" android:icon="@drawable/ic_launcher" androi

【android】夜间模式简单实现

关于阅读类的app,有个夜间模式真是太重要了. 那么有两种方式可以实现夜间模式 1:修改theme,重启activity 优点:正儿八经的夜间模式,配色看着舒服 缺点:图片刺眼.闪屏 2:使用一个带黑色带透明度的View,盖在现有的activity上,效果类似你带上墨镜,看着太阳不刺眼. 优点:不用重启activity,不闪屏:加上透明度过渡动画,模式之间切换非常舒服,解决了1中,白底图片依旧刺眼的问题.: 缺点:配色没变化,就算带上墨镜,白天依旧是白天. 因此,本方案整合了两种解决方案.在夜间

android简单的夜间模式

现在android项目values下打 attrs.xml <?xml version="1.0" encoding="utf-8"?> <resources> <attr name="bookimage" format="reference|color" /> <attr name="tvcolor" format="reference|color&qu

Android 之夜间模式(多主题)的实现思路

实现思路 多数App由于历史原因当对其进行夜间模式的功能改造时,工作量是比较大的,所以在真正开始着手实现前,我们还是应该先明确夜间模式(多主题)到底有多少实现的思路以及它们的优劣,这样才可以有效的避免项目延期的情况出现. 自定义attr属性. 这是最基础的方法,就是通过自定义attr属性,在不同的theme中进行不同的实现,然后在layout和style中调用.这种方法改代码改动量比较大,而且根据不同的主题,drawable(5.0以上不用),selector等xml文件必须要相应的定义多份,因

android 高仿网易新闻夜间模式

网易新闻有一个小功能,功能虽小,但看的出来大公司的产品设计确实很不错,今天就实现下改功能,如果用户点击选择了夜间模式或者日间模式,那么所有的activity背景都要变,其实android源码给每个activity 内容区域都定义了一个id,就是android.R.id.content,因为所有的activity背景要变色,因此肯定要写一个基类,代码如下: public class BaseActivity extends Activity { protected void onCreate(an

Android实现夜间模式小结

随着APP实现的功能越来越丰富, 看小说看视频上网等等, 如今不少人花在手机平板等移动终端上的时间越来越长了. 但手机和平板的屏幕并不像Kindle那类电纸书的水墨屏那么耐看, 因为自发光的屏幕特性, 我们长期盯着屏幕看easy眼睛酸痛疲倦, 因此各种护目模式, 夜间模式在移动APP上得到广泛应用, 这的确也是一个贴心的小功能. 所以这次我们探讨下几种实现方式, 一起学习总结下: 1, 利用屏幕亮度 当夜间使用手机等终端, 直接降低屏幕亮度, 能降低光线强度对眼镜的刺激, 这也是最简单, 也相对