您好,登录后才能下订单哦!
密码登录
登录注册
点击 登录注册 即表示同意《亿速云用户服务条款》
怎么在Android中实现切换主题?相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。
创建ColorTheme类用于主题更换:
public class ColorTheme { AppCompatActivity ap; public ColorTheme(AppCompatActivity _ap){ap=_ap;} public void updateTheme(int _data){ String data=Integer.toString(_data); FileOutputStream out=null; BufferedWriter writer=null; try{ out=ap.openFileOutput("data",Context.MODE_PRIVATE); writer=new BufferedWriter(new OutputStreamWriter(out)); writer.write(data); }catch (IOException e){ e.printStackTrace(); }finally { try { if(writer!=null){ writer.close(); } }catch (IOException e){ e.printStackTrace(); } } } public void loadTheme(){ FileInputStream in=null; BufferedReader reader= null; StringBuilder content=new StringBuilder(); try{ in=ap.openFileInput("data"); reader=new BufferedReader(new InputStreamReader(in)); String line=""; while((line=reader.readLine())!=null){ content.append(line); } ap.setTheme(Integer.parseInt(content.toString())); }catch (IOException e){ e.printStackTrace(); }finally { if(reader!=null){ try{ reader.close(); }catch (IOException e){ e.printStackTrace(); } } } } }
在oncreate中调用:
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); final ColorTheme newTheme = new ColorTheme(this); newTheme.loadTheme(); setContentView(R.layout.activity_main);
重点:
要现在res/value/style中设计主题的样式:
这里是我设的的四种样式:
<?xml version="1.0"?> <resources> <!-- Base application theme. --> -<style parent="Theme.AppCompat.Light.NoActionBar" name="AppTheme"> <!-- Customize your theme here. --> <item name="colorPrimary">@color/colorPrimary</item> <item name="colorPrimaryDark">@color/colorPrimaryDark</item> <item name="colorAccent">@color/colorAccent</item> <item name="colorButtonNormal">@color/colorAccent</item> </style> -<style parent="Theme.AppCompat.Light.NoActionBar" name="Blue"> <!-- Customize your theme here. --> <item name="colorPrimary">@color/blue</item> <item name="colorPrimaryDark">@color/blue</item> <item name="colorAccent">@color/blue</item> <item name="colorButtonNormal">@color/blue</item> </style> -<style parent="Theme.AppCompat.Light.NoActionBar" name="Pink"> <!-- Customize your theme here. --> <item name="colorPrimary">@color/pink</item> <item name="colorPrimaryDark">@color/pink</item> <item name="colorAccent">@color/pink</item> <item name="colorButtonNormal">@color/pink</item> </style> -<style parent="Theme.AppCompat.Light.NoActionBar" name="Turquoise"> <!-- Customize your theme here. --> <item name="colorPrimary">@color/turquoise</item> <item name="colorPrimaryDark">@color/turquoise</item> <item name="colorAccent">@color/turquoise</item> <item name="colorButtonNormal">@color/turquoise</item> </style> </resources>
别忘了在color里定义的颜色:
<?xml version="1.0" encoding="UTF-8"?> <resources> <color name="colorText">#ffffffff</color> <color name="hintText">#bfffffff</color> <color name="colorPrimary">#de4037</color> <color name="colorPrimaryDark">#de4037</color> <color name="colorAccent">#de4037</color> <!--注册界面提示红色--> <color name="hintRed">#de4037</color> <color name="blue">#1e50a2</color> <color name="pink">#fa7299</color> <color name="turquoise">#008577</color> </resources>
看完上述内容,你们掌握怎么在Android中实现切换主题的方法了吗?如果还想学到更多技能或想了解更多相关内容,欢迎关注亿速云行业资讯频道,感谢各位的阅读!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。