溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

怎么在Android中實現(xiàn)切換主題

發(fā)布時間:2021-05-27 17:30:39 來源:億速云 閱讀:301 作者:Leah 欄目:移動開發(fā)

怎么在Android中實現(xiàn)切換主題?相信很多沒有經(jīng)驗的人對此束手無策,為此本文總結(jié)了問題出現(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);

重點:

要現(xiàn)在res/value/style中設(shè)計主題的樣式:

這里是我設(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>
  <!--注冊界面提示紅色-->
  <color name="hintRed">#de4037</color>
  <color name="blue">#1e50a2</color>
  <color name="pink">#fa7299</color>
  <color name="turquoise">#008577</color>
</resources>

看完上述內(nèi)容,你們掌握怎么在Android中實現(xiàn)切換主題的方法了嗎?如果還想學(xué)到更多技能或想了解更多相關(guān)內(nèi)容,歡迎關(guān)注億速云行業(yè)資訊頻道,感謝各位的閱讀!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI