Android開發實現切換主題及換膚功能示例

這篇文章主要介紹了Android開發實現切換主題及換膚功能,涉及Android界面佈局與樣式動態操作相關實現技巧,需要的朋友可以參考下

本文實例講述了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相關內容感興趣的讀者可查看本站專題:《Android控件用法總結》、《Android開發入門與進階教程》、《Android視圖View技巧總結》、《Android編程之activity操作技巧總結》、《Android數據庫操作技巧總結》及《Android資源操作技巧彙總

希望本文所述對大家Android程序設計有所幫助。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章