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; @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)); } }
相关推荐
终端APP使用时,更加便利的更换主题皮肤,字体颜色也会随着主题更改而更改
fylo-dark-theme:fylo-dark-theme登陆页面主
gv-risingvoices-child-theme:gv-project-theme的子主题
资源来自pypi官网。 资源全名:sphinx-bootstrap-theme-0.4.0.tar.gz
Vimix图标主题 A Material Design icon theme Vimix图标主题基于Paper-Icon-Theme: : 安装或卸载 跑步 ./install.sh 或双击打开该脚本文件,然后在nautilus中选择“在终端运行”。 预习
jekyll-theme-nangka 欢迎使用您的新Jekyll主题! 在此目录中,您将找到能够将主题打包成gem所需的文件。 把你的布局_layouts ,你包括_includes ,在你的SASS文件_sass和任何其他资产assets 。 要尝试使用此代码...
默认情况下, tao-theme-scale-fn为tao-theme-golden-scale ,并产生以下颜色: #FCFCFC #FAFAFA #F6F6F6 #F1F1F1 #E8E8E8 #DADADA #C3C3C3 #9E9E9E #616161 #3C3C3C #252525 #171717 #0E0E0E #090909 #050505 您...
安装pydata-sphinx-theme从conda-forge通道可以通过添加实现conda-forge到您的频道: conda config --add channels conda-forge conda config --set channel_priority strict 一旦conda-forge通道已经启用, py...
vuepress-theme-hope-demo:vuepress-theme-hope的演示
用法使用开曼主题: 将以下内容添加到您网站的_config.yml : theme : jekyll-theme-cayman (可选)如果您想在计算机上预览站点,请将以下内容添加到站点的Gemfile : gem "github-pages" , group : :jekyll_...
remote_theme : fernandopasik/jekyll-theme-material-docs plugins : - jekyll-remote-theme 通过宝石 将此行添加到您的Jekyll网站的Gemfile : gem "jekyll-theme-material-docs" 并将此行添加到您的Jekyll...
vuepress-theme-hope.github.io:Vuepress-Theme-Hope的文档
Jekyll简历主题 现场演示 叠放 请通过 :star: 如果您觉得有用! :smiling_face_with_smiling_eyes: 屏幕截图 快速设置 安装Jekyll: gem install jekyll bundler 分叉此存储库并克隆您的分叉 编辑_config.yml以个性...
Theme是用于Android的实验性主题引擎,通过在创建视图后停止视图。 该库的灵感来自和 。 警告 Theme是的伴侣,因此它要求您在项目中采用material-components-android 。 目前, Theme支持material-components-...
增加Color Theme选项。 Eclipse > Windows > Preferences > General > Appearance > Color Theme(默认安装Eclipse是没有这项的) 使用方法:先关闭Eclipse。把下载好的zip文件放到Eclipse安装目录下的dropins目录...
luci-theme-netgear主题修改 直接下载主题源码放到package目录下即可 然后make menuconfig就能luci->themes查看到 软件截图
布尔主题主题 这是一个使用框架构建... theme : bulma-clean-theme 然后执行: $ bundle 或自己安装为: $ gem install bulma-clean-theme 用法 页数 将页面创建为单独的降价文件,并使用layout: page作为普通页面
vuepress:vuepress-theme-reco —个人博客
现在重新启动一 下 Emacs ,然后就可以用 M-x color-theme-select 来选择你喜欢的颜色主题 了,它会打开一个列表,在每个列表项目上回车会应用那个颜色主题,如果选上 了某个主题,按一下 d ,会出现类似: ...
Dox-盖茨比主题 用于创建文档站点的Gatsby主题。 使用Gatsby使文档变得容易。 :party_popper: 产品特点 ...在您的gatsby-config.js文件中添加gatsby-theme-dox gatsby-config.js : // gatsby-confi