Android应用Theme
Android应用Theme
Android切换Theme主流三种方式来切换Theme,第一种是通过内置的style来切换,一般用于夜间模式/日间模式切换。第二种是通过apk来实现插件化,第三种是通过下载zip进行解压到到相应的app文件下,应用讲需要文件读取到内存中。这篇是介绍第一种android切换Theme的方法。
Ubuntu 14.04 x64配置Android 4.4 kitkat编译环境的方法
Ubuntu 12.04搭建Android开发环境
Ubuntu 14.04 配置 Android SDK 开发环境
64位Ubuntu 11.10下Android开发环境的搭建(JDK+Eclipse+ADT+Android SDK详细)
Ubuntu 12.10 x64 安装 Android SDK
首先当然是在values下面创建attrs文件,然后定义了一些attr。
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="main">
<attr name="bgColor" format="color"></attr>
<attr name="buttonBgColor" format="color"></attr>
<attr name="buttonTextColor" format="color"></attr>
<attr name="textSize" format="dimension"></attr>
</declare-styleable>
</resources>
然后再在styles文件定义两个style。
01.<style name="dayTheme">
02. <item name="bgColor">#ffffff</item>
03. <item name="buttonBgColor">#80000000</item>
04. <item name="buttonTextColor">#80ffffff</item>
05. <item name="textSize">14sp</item>
06. </style>
07.
08. <style name="nightTheme">
09. <item name="bgColor">#cc000000</item>
10. <item name="buttonBgColor">#80ffffff</item>
11. <item name="buttonTextColor">#80000000</item>
12. <item name="textSize">14sp</item>
13. </style>
然后就是在布局文件中使用attrs。
<LinearLayout 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:background="?attr/bgColor"
>
<Button
android:id="@+id/swtichThemeBtn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="?attr/textSize"
android:text="按下按钮切换夜间模式"
android:layout_margin="10dip"
android:background="?attr/buttonBgColor"
android:textColor="?attr/buttonTextColor"
/>
</LinearLayout>
最后就是在mainActivity设置theme并动态切换theme。
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.view.View;
import android.widget.Button;
public class MainActivity extends Activity {
private Button mSwtichThemeBtn;
private boolean isNight;
private SharedPreferences sp;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
sp = PreferenceManager.getDefaultSharedPreferences(this);
setTheme((isNight = sp.getBoolean("isNight", false)) ? R.style.nightTheme : R.style.dayTheme);
setContentView(R.layout.activity_main);
mSwtichThemeBtn = (Button) this.findViewById(R.id.swtichThemeBtn);
mSwtichThemeBtn.setText(isNight?"切换日间模式":"切换夜间模式");
mSwtichThemeBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Editor edit = sp.edit();
edit.putBoolean("isNight", !isNight);
edit.commit();
recreateForTheme();
}
});
}
@SuppressLint("NewApi")
public void recreateForTheme(){
if(android.os.Build.VERSION.SDK_INT >= 11){
this.recreate();
}else{
this.finish();
startActivity(new Intent(MainActivity.this,MainActivity.class));
}
}
更多详情见请继续阅读下一页的精彩内容:
|
评论暂时关闭