转:Android更换皮肤-Theme方式
16lz
2022-07-14
android切换Theme主流三种方式来切换Theme,第一种是通过内置的style来切换,一般用于夜间模式/日间模式切换。第二种是通过apk来实现插件化,第三种是通过下载zip进行解压到到相应的app文件下,应用讲需要文件读取到内存中。这篇是介绍第一种android切换Theme的方法。
首先当然是在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。
<style name="dayTheme"> <item name="bgColor">#ffffff</item> <item name="buttonBgColor">#80000000</item> <item name="buttonTextColor">#80ffffff</item> <item name="textSize">14sp</item> </style> <style name="nightTheme"> <item name="bgColor">#cc000000</item> <item name="buttonBgColor">#80ffffff</item> <item name="buttonTextColor">#80000000</item> <item name="textSize">14sp</item> </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;@Overrideprotected 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() {@Overridepublic 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));}}
更多相关文章
- 【阿里云镜像】切换阿里巴巴开源镜像站镜像——Fedora镜像
- NPM 和webpack 的基础使用
- 【阿里云镜像】使用阿里巴巴DNS镜像源——DNS配置教程
- 【阿里云镜像】切换阿里巴巴开源镜像站镜像——Debian镜像
- 读取android手机流量信息
- Android(安卓)Activity界面切换添加动画特效
- android 使用html5作布局文件: webview跟javascript交互
- Android(安卓)多媒体扫描过程(Android(安卓)Media Scanner Proces
- Android系统配置数据库注释(settings.db)