zoukankan      html  css  js  c++  java
  • 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" />
    </resources>

    style.xml

    复制代码
    <?xml version="1.0" encoding="utf-8"?>
    <resources>
            <!-- 默认风格 -->
        <style name="BrowserThemeDefault" parent="@android:style/Theme.Black.NoTitleBar">
            <item name="bookimage">@android:color/white</item>
             <item name="tvcolor">@android:color/darker_gray</item>
        </style>
    
        <!-- 夜间模式 -->
        <style name="BrowserThemeNight" parent="@android:style/Theme.Black.NoTitleBar">        
            <item name="bookimage">@android:color/transparent</item>
             <item name="tvcolor">@android:color/white</item>
        </style>
    </resources>
    复制代码

    layout下activity_main

    复制代码
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    
        android:layout_width="match_parent"
        android:layout_height="match_parent"
    //界面颜色改变 android:background="?bookimage" > <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/hello_world"
    //字体颜色改变 android:textColor="?tvcolor"/> <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_centerVertical="true"
    //监听方法
    android:onClick="btonclick" android:text="日/夜间模式切换" /> <Button android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/textView1" android:layout_centerHorizontal="true" android:layout_marginTop="116dp" android:onClick="btonclick2" android:text="跳转其他页面" /> </RelativeLayout>
    复制代码

    MainActivity

    复制代码
    package com.example.zdndemo;
    
    
    
    import android.os.Bundle;
    import android.app.Activity;
    import android.content.Intent;
    import android.content.SharedPreferences;
    import android.content.SharedPreferences.Editor;
    import android.view.Menu;
    import android.view.MenuInflater;
    import android.view.MenuItem;
    import android.view.View;
    
    public class MainActivity extends Activity {
        private static boolean blFlag = false;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            
            
        
            SharedPreferences preferences = getSharedPreferences("default_night",
                    MODE_PRIVATE);
               blFlag = preferences.getBoolean("default_night",true);
            if (blFlag) {
                 this.setTheme(R.style.BrowserThemeDefault);
            }      
            else {
                this.setTheme(R.style.BrowserThemeNight);
            } 
            //上面的代码必须要放在setContentView之上
            
            setContentView(R.layout.activity_main);
        }
        
        public void btonclick(View view) {
            SharedPreferences preferences = getSharedPreferences("default_night",MODE_PRIVATE);
            Editor editor = preferences.edit();
            if (blFlag) {
                this.setTheme(R.style.BrowserThemeNight);
                blFlag =false;
                editor.putBoolean("default_night",false);
            } else {
                this.setTheme(R.style.BrowserThemeDefault);
                blFlag = true;
                editor.putBoolean("default_night",true);
                
            }                   
          // 提交修改
             editor.commit();  
            this.setContentView(R.layout.activity_main);
    //不行的话在跳下本页面
    } public void btonclick2(View view) { Intent intent = new Intent(); intent.setClass(this, breakactivity.class); startActivity(intent); } }
    复制代码
  • 相关阅读:
    Helvetic Coding Contest 2017 online mirror (teams allowed, unrated) J
    ROS_Kinetic_19 群机器人框架示例(micros swarm framework)
    ROS_Kinetic_18 使用V-Rep3.3.1和Matlab2015b(vrep_ros_bridge)续
    ROS_Kinetic_17 使用V-Rep3.3.1(vrep_ros_bridge)
    USB OTG原理+ ID 检测原理
    高通QSD MSM APQ区别
    qualcomm memory dump 抓取方法
    msm8974 camera driver添加新摄像头kernel hal修改
    现代控制理论-章节组织结构和仿真应用案例详细分析
    ROS_Kinetic_16 ubuntu中安装使用Matlab和ROS
  • 原文地址:https://www.cnblogs.com/wbp0818/p/5320703.html
Copyright © 2011-2022 走看看