首先,activity本身是一个窗体,他主要负责的是视图,可以给他添加视图或者删除视图,然后有一个蒙版覆盖在上面,要给activity加一层蒙版, 这时候我们需要先初始化蒙版,在初始化蒙版时候,要先得到系统的WindowManager,然后再setLayoutParams,而LayoutParams需要实例化一个WindowManager.LayoutParams,这个方法里面有三个参数, 分别是1.布局
文件类型 2.flag(标志),设置无焦点,不可触摸 3.图像格式,透明。这三个参数根据情况而定。其中就是透明度的变化,可以给他进行设置,然后是夜间模式的切换,我们可以写一个方法,如果是夜间模式就添加到view中,如果不是就立即移除。最后点击切换按钮时候,切换到夜间模式就行了。其中一个要实现自定义的activity。最后在保存一下夜间模式的状态,用SharedPreferences保存,先得到getSharedPreferences,然后获得编辑器sp.edit(),然后传putBoolean(),最后提交commit(),这样就可以了。
代码:
MainActivity中:
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import java.util.ArrayList;
import java.util.List;
/**
* 夜间模式的切换,字体颜色的改变
*/
public class MainActivityextendsBaseActivity {
private ListView mListview;
private List<Demo> list;
private Button mButton;
private boolean isNight=false;
private ImageView imgDay;
private LinearLayout layout;
@Override
protected voidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//初始化view
mListview= (ListView) findViewById(R.id.list_view);
mButton = (Button) findViewById(R.id.btn_switch);
imgDay= (ImageView) findViewById(R.id.day);
layout = (LinearLayout) findViewById(R.id.layout);
//初始化数据
list=new ArrayList<>();
list.add(newDemo(true,"还让所有", R.mipmap.ic_launcher));
list.add(newDemo(true,"不太舒服的柔软月考", R.mipmap.ic_launcher));
list.add(newDemo(false,"裤日违反"));
list.add(newDemo(true,"格外收入的结构", R.mipmap.ic_launcher));
list.add(newDemo(false,"哈尔一般"));
list.add(newDemo(true,"合同预计", R.mipmap.ic_launcher));
list.add(newDemo(false,"好风如水没法"));
list.add(newDemo(true,"开发与你说这个", R.mipmap.ic_launcher));
list.add(newDemo(true,"好温柔和台湾", R.mipmap.ic_launcher));
list.add(newDemo(false,"还让我说而阿根廷与"));
list.add(newDemo(false,"还让我说而阿根廷与"));
list.add(newDemo(true,"红烧肉投入其他", R.mipmap.ic_launcher));
list.add(newDemo(true,"红烧肉投入其他", R.mipmap.ic_launcher));
list.add(newDemo(true,"红烧肉投入其他", R.mipmap.ic_launcher));
//设置适配器
mListview.setAdapter(newDemoAdapter(this,list));
mButton.setOnClickListener(newView.OnClickListener() {
@Override
public voidonClick(View v) {
isNight = !isNight;
BaseApplication.getIntance().setNight(isNight);
switchMode(isNight);
得到Window的视图
View view = getWindow().getDecorView();
changeTextColor((ViewGroup) view);
changeImage();
}
});
}
/**
* 日夜间模式切换的时候改变自身的图片
*/
private voidchangeImage() {
imgDay.setImageResource(BaseApplication.getIntance().getResId(R.mipmap.b_newcare_tabbar));
}
// 在视图可见可交互的时候才能改变视图
@Override
protected voidonResume() {
super.onResume();
// 一进入Activity,先拿到全局的isNight
isNight= BaseApplication.getIntance().isNight();
if (isNight) {
switchMode(isNight);
}
changeTextColor((ViewGroup) getWindow().getDecorView());
changeImage();
}
}
activity_main中:
<?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:id="@+id/layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.bawei.liujie.nightmode.MainActivity">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/return_text"
android:layout_width="wrap_content"
android:text="返回"
android:padding="5dp"
android:textColor="#000"
android:layout_height="wrap_content" />
<ImageView
android:id="@+id/day"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_alignParentRight="true"
android:background="@mipmap/b_newcare_tabbar"/>
</RelativeLayout>
<Button
android:id="@+id/btn_switch"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="切换夜间模式"/>
<ListView
android:id="@+id/list_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"></ListView>
</LinearLayout>
Demo类:
public class Demo {
private boolean hsgImg;
private String name;
private int id;
public Demo() {
}
public Demo(boolean hsgImg, String name) {
this.hsgImg = hsgImg;
this.name = name;
}
public Demo(boolean hsgImg, String name, int id) {
this.hsgImg = hsgImg;
this.id = id;
this.name = name;
}
public boolean isHsgImg() {
return hsgImg;
}
public void setHsgImg(boolean hsgImg) {
this.hsgImg = hsgImg;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
DemoAdapter:
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.List;
public class DemoAdapter extends BaseAdapter {
private Context context;
private List<Demo> list;
private static final int TYPE_HAS_PIC = 0;
private static final int TYPE_NO_PIC = 1;
public DemoAdapter(Context context, List<Demo> list) {
this.context = context;
this.list = list;
}
@Override
public int getCount() {
return list.size();
}
@Override
public Object getItem(int position) {
return list.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public int getViewTypeCount() {
return 2;
}
@Override
public int getItemViewType(int position) {
boolean type=list.get(position).isHsgImg();
if(type){
return TYPE_HAS_PIC;
}else{
return TYPE_NO_PIC;
}
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
int type=getItemViewType(position);
if (convertView == null) {
holder = new ViewHolder();
switch (type){
case TYPE_HAS_PIC:
convertView = View.inflate(context, R.layout.item, null);
holder.icon = (ImageView) convertView.findViewById(R.id.icon);
holder.text = (TextView) convertView.findViewById(R.id.text_view);
break;
case TYPE_NO_PIC:
convertView = View.inflate(context, R.layout.item2, null);
holder.text = (TextView) convertView.findViewById(R.id.text_view);
break;
}
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
switch (type){
case TYPE_HAS_PIC:
holder.icon.setImageResource(list.get(position).getId());
holder.text.setText(list.get(position).getName());
break;
case TYPE_NO_PIC:
holder.text.setText(list.get(position).getName());
break;
}
return convertView;
}
class ViewHolder {
ImageView icon;
TextView text;
}
}
item:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<ImageView
android:id="@+id/icon"
android:layout_width="50dp"
android:layout_height="50dp" />
<TextView
android:id="@+id/text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"/>
</LinearLayout>
item2:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"/>
</LinearLayout>
BaseApplication: import android.app.Application; import android.content.SharedPreferences; import android.graphics.Color; import java.util.HashMap; import java.util.Map; public class BaseApplication extends Application { private static BaseApplication application; private boolean isNight = true; // 是否是夜间模式 private SharedPreferences sp; // map存值,key=日间模式的图, value=夜间模式的图 private Map<Integer, Integer> map; @Override public void onCreate() { super.onCreate(); application = this; sp = getSharedPreferences("config", MODE_PRIVATE); isNight = sp.getBoolean("is_night", false); } /** * 类似单例模式,返回的是当前Application的对象 * @return */ public static BaseApplication getIntance() { return application; } public boolean isNight() { return isNight; } /** * 初始化颜色和图片的集合 */ private void initImages() { map=new HashMap<>(); map.put(R.mipmap.b_newcare_tabbar, R.mipmap.b_newcare_tabbar_press); map.put(Color.BLUE, Color.YELLOW); } /** * 日夜间模式分别得到不同的资源文件 * @param resId * @return */ public int getResId(int resId) { if (map == null) { initImages(); } // 夜间模式,显示value的资源 if (isNight) { return map.get(resId); } else { // 日间模式,显示key的资源 return resId; } } public void setNight(boolean night) { isNight = night; //设置夜间模式的时候保存到本地 sp.edit().putBoolean("is_night",night).commit(); } }
BaseActivity:
import android.graphics.Color;import android.graphics.PixelFormat;import android.os.Bundle;import android.support.v7.app.AppCompatActivity;import android.view.View;import android.view.ViewGroup;import android.view.WindowManager;import android.widget.TextView;public class BaseActivity extends AppCompatActivity { private WindowManager.LayoutParams params; private WindowManager manager; private View view; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_base); //初始化蒙版 initPreView(); } /** * 切换夜间模式 */ public void switchMode(boolean isNight) { if (isNight) { //夜间模式的时候添加 manager.addView(view,params); }else{ //非夜间模式的时候移除 manager.removeView(view); } } /** * 改变文字的颜色 * @param viewGroup */ public void changeTextColor(ViewGroup viewGroup) { for (int i = 0; i < viewGroup.getChildCount(); i++) { // 判断子控件是否是textview if (viewGroup.getChildAt(i) instanceof TextView) { TextView txt = (TextView) viewGroup.getChildAt(i); // 得到模式的颜色值 int resId = BaseApplication.getIntance().getResId(Color.BLUE); txt.setTextColor(resId); } else if(viewGroup.getChildAt(i) instanceof ViewGroup){// viewGroup.getChildAt(i).setBackgroundColor(Color.BLACK); // 递归调用 changeTextColor((ViewGroup) viewGroup.getChildAt(i)); } } } //初始化蒙版 private void initPreView() { view = new View(this); view.setBackgroundColor(getResources().getColor(R.color.colorWindow)); //得到系统的WindowManager manager = (WindowManager) getSystemService(WINDOW_SERVICE); //布局参数:1.布局文件类型 2.flag,设置无焦点,不可触摸 3.图像格式,透明 params = new WindowManager.LayoutParams(WindowManager.LayoutParams.TYPE_APPLICATION , WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE, PixelFormat.TRANSLUCENT);// view.setLayoutParams(params); }}