点击 Night后,主题皮肤
[java] view plain copy
-
package cn.hpc.assistant;
-
import android.app.Activity;
-
import android.content.Context;
-
import android.graphics.PixelFormat;
-
import android.graphics.Point;
-
import android.os.Bundle;
-
import android.os.Handler;
-
import android.os.Message;
-
import android.view.Gravity;
-
import android.view.View;
-
import android.view.WindowManager;
-
import android.view.WindowManager.LayoutParams;
-
public class MainActivity extends Activity {
-
private int mThemeId = -1; // 皮肤主题ID,默认-1 不处理
-
@Override
-
protected void onCreate(Bundle savedInstanceState) {
-
super.onCreate(savedInstanceState);
-
if (savedInstanceState != null) {
-
if (savedInstanceState.getInt(“theme”, -1) != -1) {// 读取皮肤主题ID,-1 不处理
-
mThemeId = savedInstanceState.getInt(“theme”);
-
this.setTheme(mThemeId); //设置主题皮肤
-
}
-
}
-
setContentView(R.layout.activity_main);
-
this.findViewById(R.id.id_btn_day).setOnClickListener(mOnClickListener); // day 明亮主题
-
this
《Android学习笔记总结+最新移动架构视频+大厂安卓面试真题+项目实战源码讲义》
【docs.qq.com/doc/DSkNLaERkbnFoS0ZF】 完整内容开源分享
.findViewById(R.id.id_btn_night).setOnClickListener(mOnClickListener); //night 黑暗主题
-
}
-
View.OnClickListener mOnClickListener = new View.OnClickListener() {
-
@Override
-
public void onClick(View v) {
-
// TODO Auto-generated method stub
-
switch (v.getId()) {
-
case R.id.id_btn_day:
-
onTheme(android.R.style.Theme_Light);
-
break;
-
case R.id.id_btn_night:
-
onTheme(android.R.style.Theme_Black);
-
break;
-
default:
-
}
-
}
-
};
-
// 设置主题,并重建
-
private void onTheme(int iThemeId){
-
mThemeId = iThemeId;
-
this.recreate();
-
}
-
// 保存主题ID,onCreate 时读取主题
-
@Override
-
public void onSaveInstanceState(Bundle outState) {
-
super.onSaveInstanceState(outState);
-
outState.putInt(“theme”, mThemeId);
-
}
-
}
布局文件:
activity_main.xml
[html] view plain copy
-
<LinearLayout xmlns:android=“http://schemas.android.com/apk/res/android”
-
xmlns:tools=“http://schemas.android.com/tools”
-
android:layout_width=“wrap_content”
-
android:layout_height=“wrap_content”
-
android:orientation=“horizontal”
-
tools:context=".MainActivity" >
-
<TextView
-
android:layout_width=“wrap_content”
-
android:layout_height=“wrap_content”
-
android:layout_centerHorizontal=“true”
-
android:layout_centerVertical=“true”
-
android:text="@string/hello_world" />
-
<Button
-
android:id="@+id/id_btn_fun"
-
android:layout_width=“wrap_content”
-
android:layout_height=“wrap_content” />
-
<Button
-
android:id="@+id/id_btn_day"
-
android:layout_width=“wrap_content”
yout_width=“wrap_content”
-
android:layout_height=“wrap_content” />
-
<Button
-
android:id="@+id/id_btn_day"
-
android:layout_width=“wrap_content”