android應用Theme(一)

來源:互聯網
上載者:User

標籤:android   theme   attrs   styles   

android切換Theme主流三種方式來切換Theme,第一種是通過內建的style來切換,一般用於夜間模式/日間模式切換。第二種是通過apk來實現外掛程式化,第三種是通過下載zip進行解壓到到相應的app檔案下,應用講需要檔案讀取到記憶體中。這篇是介紹第一種android切換Theme的方法。

首先當然是在values下面建立attrs檔案,然後定義了一些attr。

<?xml version="1.0" encoding="utf-8"?><resources>  <declare-styleable name="main">      <attr name="bgColor" format="color"></attr>      <attr name="buttonBgColor" format="color"></attr>       <attr name="buttonTextColor" format="color"></attr>      <attr name="textSize" format="dimension"></attr>      </declare-styleable>  </resources>
然後再在styles檔案定義兩個style。

<style name="dayTheme">        <item name="bgColor">#ffffff</item>        <item name="buttonBgColor">#80000000</item>        <item name="buttonTextColor">#80ffffff</item>        <item name="textSize">14sp</item>    </style>          <style name="nightTheme">        <item name="bgColor">#cc000000</item>        <item name="buttonBgColor">#80ffffff</item>          <item name="buttonTextColor">#80000000</item>        <item name="textSize">14sp</item>    </style>
然後就是在布局檔案中使用attrs。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:background="?attr/bgColor"    >   <Button       android:id="@+id/swtichThemeBtn"       android:layout_width="match_parent"       android:layout_height="wrap_content"       android:textSize="?attr/textSize"       android:text="按下按鈕切換夜間模式"       android:layout_margin="10dip"       android:background="?attr/buttonBgColor"       android:textColor="?attr/buttonTextColor"       /></LinearLayout>

最後就是在mainActivity設定theme並動態切換theme。

import android.os.Bundle;import android.preference.PreferenceManager;import android.annotation.SuppressLint;import android.app.Activity;import android.content.Intent;import android.content.SharedPreferences;import android.content.SharedPreferences.Editor;import android.view.View;import android.widget.Button;public class MainActivity extends Activity {private Button mSwtichThemeBtn;private boolean isNight;private SharedPreferences sp;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);sp = PreferenceManager.getDefaultSharedPreferences(this);setTheme((isNight = sp.getBoolean("isNight", false)) ? R.style.nightTheme : R.style.dayTheme);setContentView(R.layout.activity_main);mSwtichThemeBtn = (Button) this.findViewById(R.id.swtichThemeBtn);mSwtichThemeBtn.setText(isNight?"切換日間模式":"切換夜間模式");mSwtichThemeBtn.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {Editor edit = sp.edit();edit.putBoolean("isNight", !isNight);edit.commit();recreateForTheme();}});}@SuppressLint("NewApi")public void recreateForTheme(){if(android.os.Build.VERSION.SDK_INT >= 11){this.recreate();}else{this.finish();startActivity(new Intent(MainActivity.this,MainActivity.class));}}


    




聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.