android基礎(chǔ)教程之夜間模式實(shí)現(xiàn)示例
package org.david.dayandnightdemo.cor;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.graphics.Color;
import android.graphics.PixelFormat;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager;
import android.view.WindowManager.LayoutParams;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity implements OnClickListener {
private WindowManager mWindowManager;
private View myView;
private Button btn_dayAndnight;
private SharedPreferences skinSp;
private final static String DAY = "day";
private final static String NIGHT = "night";
private int flage = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mWindowManager = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
setContentView(R.layout.activity_main);
init();
}
private void init() {
skinSp = this.getSharedPreferences("skinchange", Context.MODE_PRIVATE);
btn_dayAndnight = (Button) findViewById(R.id.btn_dayAndnight);
btn_dayAndnight.setOnClickListener(this);
String mode = skinSp.getString("skin", "");
if(mode!=null||!mode.equals("")){
if(mode.equals(NIGHT)){
night();
}else{
day();
}
}
}
@Override
public void onClick(View v) {
if(flage%2==0){
night();
btn_dayAndnight.setText("白天模式");
btn_dayAndnight.setTextColor(Color.WHITE);
flage++;
}else{
day();
btn_dayAndnight.setText("夜間模式");
btn_dayAndnight.setTextColor(Color.BLACK);
flage++;
}
}
public void night() {
WindowManager.LayoutParams params = new WindowManager.LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT,
LayoutParams.TYPE_APPLICATION,
WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE
| WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
PixelFormat.TRANSLUCENT);
params.gravity=Gravity.BOTTOM;
params.y=10;
if(myView==null){
myView=new TextView(this);
myView.setBackgroundColor(0x80000000);
}
mWindowManager.addView(myView, params);
Editor edit = skinSp.edit();
edit.putString("skin", NIGHT);
edit.commit();
}
public void day(){
if(myView!=null){
mWindowManager.removeView(myView);
Editor edit = skinSp.edit();
edit.putString("skin", DAY);
edit.commit();
}
}
public void removeSkin(){
if(myView!=null){
mWindowManager.removeView(myView);
}
}
@Override
protected void onDestroy() {
super.onDestroy();
String mode = skinSp.getString("skin", "");
if(mode.equals(NIGHT)){
removeSkin();
}
}
}
布局文件
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<Button
android:id="@+id/btn_dayAndnight"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/btn_changeskin" />
</RelativeLayout>
- Android實(shí)現(xiàn)日夜間模式的深入理解
- 三行Android代碼實(shí)現(xiàn)白天夜間模式流暢切換
- Android夜間模式最佳實(shí)踐
- Android實(shí)現(xiàn)夜間模式切換功能實(shí)現(xiàn)代碼
- Android 實(shí)現(xiàn)夜間模式的快速簡單方法實(shí)例詳解
- Android主題切換之探究白天和夜間模式
- Android編程實(shí)現(xiàn)夜間模式的方法小結(jié)
- Android 夜間模式的實(shí)現(xiàn)代碼示例
- AndroidSDK Support自帶夜間、日間模式切換詳解
- Android如何快速適配暗黑模式詳解
相關(guān)文章
android實(shí)現(xiàn)定時(shí)拍照功能
這篇文章主要為大家詳細(xì)介紹了android實(shí)現(xiàn)定時(shí)拍照功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-05-05
AndroidManifest.xml <uses-feature>和<uses-permisstio
這篇文章主要介紹了AndroidManifest.xml <uses-feature>和<uses-permisstion>分析及比較的相關(guān)資料,需要的朋友可以參考下2017-06-06
Android筆記之:深入為從右向左語言定義復(fù)雜字串的詳解
本篇文章是對Android中為從右向左語言定義復(fù)雜字串進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-05-05
學(xué)習(xí)使用Material Design控件(三)使用CardView實(shí)現(xiàn)卡片效果
這篇文章主要為大家介紹了學(xué)習(xí)使用Material Design控件的詳細(xì)教程,如何使用CardView實(shí)現(xiàn)卡片效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-07-07
Android實(shí)現(xiàn)圖片輪播切換實(shí)例代碼
利用Android的ViewFlipper和AnimationUtils實(shí)現(xiàn)圖片帶有動(dòng)畫的輪播切換,其中當(dāng)點(diǎn)擊“上一張”圖片時(shí),切換到上一張圖片;當(dāng)點(diǎn)擊“下一張”圖片時(shí),切換到下一張圖片,本文給大家介紹Android實(shí)現(xiàn)圖片輪播切換實(shí)例代碼,需要的朋友參考下2015-12-12
Android使用ViewPager實(shí)現(xiàn)滾動(dòng)廣告
這篇文章主要為大家詳細(xì)介紹了Android使用ViewPager實(shí)現(xiàn)滾動(dòng)廣告,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-11-11
Android基于reclyview實(shí)現(xiàn)列表回彈動(dòng)畫效果
這篇文章主要為大家詳細(xì)介紹了Android基于reclyview實(shí)現(xiàn)列表回彈動(dòng)畫效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-04-04
Android開發(fā)中setContentView和inflate的區(qū)別分析
這篇文章主要介紹了Android開發(fā)中setContentView和inflate的區(qū)別,較為詳細(xì)的分析了setContentView和inflate的功能、用法及二者的區(qū)別,需要的朋友可以參考下2016-07-07

