很久没有写blog了,也不知道是怎么会事,每次想要写的时候,刚打开,就不知道写什么了,准备好的东西,也感觉没兴趣。可能还是有点懒惰吧。
想了想,blog还是要写的,不管怎样!昨天无聊,就随便写了个小例子,看看有没有用的到的,呵呵!
1 package org.david.dayandnightdemo.cor; 2 3 import android.os.Bundle; 4 import android.app.Activity; 5 import android.content.Context; 6 import android.content.SharedPreferences; 7 import android.content.SharedPreferences.Editor; 8 import android.graphics.Color; 9 import android.graphics.PixelFormat; 10 import android.view.Gravity; 11 import android.view.View; 12 import android.view.View.OnClickListener; 13 import android.view.WindowManager; 14 import android.view.WindowManager.LayoutParams; 15 import android.widget.Button; 16 import android.widget.TextView; 17 18 public class MainActivity extends Activity implements OnClickListener { 19 20 private WindowManager mWindowManager; 21 private View myView; 22 private Button btn_dayAndnight; 23 private SharedPreferences skinSp; 24 private final static String DAY = "day"; 25 private final static String NIGHT = "night"; 26 private int flage = 0; 27 28 @Override 29 protected void onCreate(Bundle savedInstanceState) { 30 super.onCreate(savedInstanceState); 31 mWindowManager = (WindowManager) getSystemService(Context.WINDOW_SERVICE); 32 setContentView(R.layout.activity_main); 33 init(); 34 } 35 36 private void init() { 37 skinSp = this.getSharedPreferences("skinchange", Context.MODE_PRIVATE); 38 btn_dayAndnight = (Button) findViewById(R.id.btn_dayAndnight); 39 btn_dayAndnight.setOnClickListener(this); 40 41 String mode = skinSp.getString("skin", ""); 42 if(mode!=null||!mode.equals("")){ 43 if(mode.equals(NIGHT)){ 44 night(); 45 }else{ 46 day(); 47 } 48 } 49 50 } 51 52 @Override 53 public void onClick(View v) { 54 if(flage%2==0){ 55 night(); 56 btn_dayAndnight.setText("白天模式"); 57 btn_dayAndnight.setTextColor(Color.WHITE); 58 flage++; 59 }else{ 60 day(); 61 btn_dayAndnight.setText("夜间模式"); 62 btn_dayAndnight.setTextColor(Color.BLACK); 63 flage++; 64 } 65 } 66 67 public void night() { 68 WindowManager.LayoutParams params = new WindowManager.LayoutParams( 69 LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT, 70 LayoutParams.TYPE_APPLICATION, 71 WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE 72 | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE, 73 PixelFormat.TRANSLUCENT); 74 params.gravity=Gravity.BOTTOM; 75 params.y=10; 76 if(myView==null){ 77 myView=new TextView(this); 78 myView.setBackgroundColor(0x80000000); 79 } 80 mWindowManager.addView(myView, params); 81 Editor edit = skinSp.edit(); 82 edit.putString("skin", NIGHT); 83 edit.commit(); 84 } 85 86 public void day(){ 87 if(myView!=null){ 88 mWindowManager.removeView(myView); 89 Editor edit = skinSp.edit(); 90 edit.putString("skin", DAY); 91 edit.commit(); 92 } 93 } 94 95 96 public void removeSkin(){ 97 if(myView!=null){ 98 mWindowManager.removeView(myView); 99 } 100 } 101 102 @Override 103 protected void onDestroy() { 104 super.onDestroy(); 105 String mode = skinSp.getString("skin", ""); 106 if(mode.equals(NIGHT)){ 107 removeSkin(); 108 } 109 } 110 111 }
布局文件:
1 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 xmlns:tools="http://schemas.android.com/tools" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 android:paddingBottom="@dimen/activity_vertical_margin" 6 android:paddingLeft="@dimen/activity_horizontal_margin" 7 android:paddingRight="@dimen/activity_horizontal_margin" 8 android:paddingTop="@dimen/activity_vertical_margin" 9 tools:context=".MainActivity" > 10 11 <Button 12 android:id="@+id/btn_dayAndnight" 13 android:layout_width="fill_parent" 14 android:layout_height="wrap_content" 15 android:text="@string/btn_changeskin" /> 16 17 </RelativeLayout>
共勉:时间像海绵里的水,挤一挤还总是会有的,呵呵。。。