Android開發(fā)實現(xiàn)切換主題及換膚功能示例
本文實例講述了Android開發(fā)實現(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中調用:
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); final ColorTheme newTheme = new ColorTheme(this); newTheme.loadTheme(); setContentView(R.layout.activity_main);
重點:
要現(xiàn)在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相關內容感興趣的讀者可查看本站專題:《Android控件用法總結》、《Android開發(fā)入門與進階教程》、《Android視圖View技巧總結》、《Android編程之activity操作技巧總結》、《Android數(shù)據庫操作技巧總結》及《Android資源操作技巧匯總》
希望本文所述對大家Android程序設計有所幫助。
相關文章
Android動態(tài)使用VectorDrawable過程詳解
這篇文章主要介紹了Android動態(tài)使用VectorDrawable過程,2014年6月26日的I/O 2014開發(fā)者大會上谷歌正式推出了Android L,它帶來了全新的設計語言Material Design,新的API也提供了這個類VectorDrawable2023-02-02Android Animation之TranslateAnimation(平移動畫)
這篇文章主要為大家詳細介紹了Animation之TranslateAnimation平移動畫,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-09-09Android計時器的三種實現(xiàn)方式(Chronometer、Timer、handler)
這篇文章主要介紹了Android計時器的三種實現(xiàn)方式,Chronometer、Timer、handler計時器的實現(xiàn)方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-11-11Android 屬性動畫ValueAnimator與插值器詳解
這篇文章主要介紹了Android 屬性動畫ValueAnimator與插值器詳解的相關資料,需要的朋友可以參考下2017-05-05Android編程開發(fā)之RadioGroup用法實例
這篇文章主要介紹了Android編程開發(fā)之RadioGroup用法,結合實例形式分析了Android中RadioGroup單選按鈕的具體使用技巧,需要的朋友可以參考下2015-12-12Flutter app頁面路由以及路由攔截的實現(xiàn)
本篇介紹了介紹了Flutter如何使用路由來實現(xiàn)頁面的跳轉,從而簡化頁面之間的耦合,并可以實現(xiàn)路由攔截。2021-06-06