Android開發(fā)實(shí)現(xiàn)切換主題及換膚功能示例
本文實(shí)例講述了Android開發(fā)實(shí)現(xiàn)切換主題及換膚功能。分享給大家供大家參考,具體如下:
廢話不說先看效果:

創(chuàng)建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中調(diào)用:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
final ColorTheme newTheme = new ColorTheme(this);
newTheme.loadTheme();
setContentView(R.layout.activity_main);
重點(diǎn):
要現(xiàn)在res/value/style中設(shè)計(jì)主題的樣式:
這里是我設(shè)的的四種樣式:
<?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> <!--注冊(cè)界面提示紅色--> <color name="hintRed">#de4037</color> <color name="blue">#1e50a2</color> <color name="pink">#fa7299</color> <color name="turquoise">#008577</color> </resources>
更多關(guān)于Android相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Android控件用法總結(jié)》、《Android開發(fā)入門與進(jìn)階教程》、《Android視圖View技巧總結(jié)》、《Android編程之a(chǎn)ctivity操作技巧總結(jié)》、《Android數(shù)據(jù)庫(kù)操作技巧總結(jié)》及《Android資源操作技巧匯總》
希望本文所述對(duì)大家Android程序設(shè)計(jì)有所幫助。
相關(guān)文章
Kotlin?協(xié)程的取消機(jī)制詳細(xì)解讀
這篇文章主要為大家介紹了Kotlin?協(xié)程的取消機(jī)制詳細(xì)解讀,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-10-10
Android 列表選擇框 Spinner詳解及實(shí)例
這篇文章主要介紹了Android 列表選擇框 Spinner詳解及實(shí)例的相關(guān)資料,需要的朋友可以參考下2017-06-06
Android編譯出現(xiàn)Warning:Mapping?new?ns?to?old?ns報(bào)錯(cuò)的解決方案
android在編譯的過程中難免會(huì)出現(xiàn)些錯(cuò)誤,下面這篇文章主要給大家介紹了關(guān)于Android編譯出現(xiàn)Warning:Mapping?new?ns?to?old?ns報(bào)錯(cuò)的解決方案,需要的朋友可以參考下2023-02-02
Android實(shí)現(xiàn)點(diǎn)擊兩次BACK鍵退出應(yīng)用
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)點(diǎn)擊兩次BACK鍵退出應(yīng)用的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-09-09
android開發(fā)實(shí)現(xiàn)文件讀寫
這篇文章主要為大家詳細(xì)介紹了android開發(fā)實(shí)現(xiàn)文件讀寫,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-07-07
基于Android FileProvider 屬性配置詳解及FileProvider多節(jié)點(diǎn)問題
這篇文章主要介紹了基于Android FileProvider 屬性配置詳解及FileProvider多節(jié)點(diǎn)問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-03-03

