有這麼一個需求,能夠對頁面的樣式進行選擇,而後根據選擇改變程序全部字體顏色和頁面背景。同時下一次啓動程序,當前設置依然有效。java
根據需求,咱們須要一種快速,方便,有效的方式來實現需求,而後能夠經過Android Them + SharedPreferences 來實現需求。Them用於存放設置的每一種樣式,並應用於程序中,SharedPreferences用於記住程序當前的樣式,根據SharedPreferences的內容來設置程序的樣式,實現下次啓動可以oncreat當前的樣式設置。android
這裏的Them比較簡單,只是定義了字體顏色和頁面背景顏色。在res/values/styles.xml 文件中增長Them主題app
<style name="FirstThem"> <item name="android:textColor">@color/FirstThemTextColor</item> <!-- 字體顏色 --> <item name="android:windowBackground">@color/FirstThemBackgroundColor</item> <!-- 窗口背景 --> </style> <style name="SecondThem"> <item name="android:textColor">@color/SecondThemTextColor</item> <!-- 字體顏色 --> <item name="android:windowBackground">@color/SecondThemBackgroundColor</item> <!-- 窗口背景 --> </style> <style name="ThirdThem"> <item name="android:textColor">@color/ThirdThemTextColor</item> <!-- 字體顏色 --> <item name="android:windowBackground">@color/ThirdThemBackgroundColor</item> <!-- 窗口背景 --> </style>
而後在MainActivity.java中建立SharedPreferences來記錄樣式的狀態ide
private void SharePreference() { sharePrefences=this.getSharedPreferences("config",Context.MODE_WORLD_READABLE | Context.MODE_WORLD_WRITEABLE); editor=sharePrefences.edit(); boolean isThem = sharePrefences.getBoolean("isThem", false); int Them = sharePrefences.getInt("Them",0);//config不存在時返回0 if(isThem){ if(Them==1){ setTheme(R.style.FirstThem); }else if(Them==2){ setTheme(R.style.SecondThem); }else if(Them==3){ setTheme(R.style.ThirdThem); } }else{//sharePrefences不存在是使用默認主題 setTheme(R.style.FirstThem); } }
有兩個比較值得注意的地方是:佈局
一、設置主題時,setTheme(R.style.FirstThem);必定要放在setContentView(R.layout.activity_main);前,不然無效。字體
setTheme(R.style.FirstThem);
setContentView(R.layout.activity_main);
二、要全部頁面的字體顏色和背景可以根據Them去改變,那麼佈局文件中的目標控件都不能設置android:textcolor,以及android:background.不然控件的android:textcolor,android:background屬性會將Them的設置覆蓋。this
下面是一個demo的完整代碼:spa
activity_main.xmlcode
<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:orientation="vertical" > <Button android:id="@+id/FirstThem" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="樣式一" /> <Button android:id="@+id/SecondThem" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="樣式二" /> <Button android:id="@+id/ThirdThem" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="樣式三" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:layout_marginTop="50dp" android:textSize="30sp" android:text="ABCDEFG" /> </LinearLayout>
MainActivity.javaxml
public class MainActivity extends Activity implements OnClickListener { private Button FirstThemButton; private Button SecondThemButton; private Button ThirdThemButton; private SharedPreferences sharePrefences; private Editor editor; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); SharePreference(); setContentView(R.layout.activity_main); InitView(); FirstThemButton=(Button) findViewById(R.id.FirstThem); SecondThemButton=(Button) findViewById(R.id.SecondThem); SecondThemButton=(Button) findViewById(R.id.ThirdThem); } private void SharePreference() { sharePrefences=this.getSharedPreferences("config",Context.MODE_WORLD_READABLE | Context.MODE_WORLD_WRITEABLE); editor=sharePrefences.edit(); boolean isThem = sharePrefences.getBoolean("isThem", false); int Them = sharePrefences.getInt("Them",0);//config不存在時返回0 if(isThem){ if(Them==1){ setTheme(R.style.FirstThem); }else if(Them==2){ setTheme(R.style.SecondThem); }else if(Them==3){ setTheme(R.style.ThirdThem); } }else{//sharePrefences不存在是使用默認主題 setTheme(R.style.FirstThem); } } private void InitView() { FirstThemButton=(Button) findViewById(R.id.FirstThem); SecondThemButton=(Button) findViewById(R.id.SecondThem); ThirdThemButton=(Button) findViewById(R.id.ThirdThem); FirstThemButton.setOnClickListener(this); SecondThemButton.setOnClickListener(this); ThirdThemButton.setOnClickListener(this); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.FirstThem: editor.putBoolean("isThem", true); editor.putInt("Them", 1); editor.commit(); Intent intent1=new Intent(this,MainActivity.class); startActivity(intent1); break; case R.id.SecondThem: editor.putBoolean("isThem", true); editor.putInt("Them",2); editor.commit(); Intent intent2=new Intent(this,MainActivity.class); startActivity(intent2); break; case R.id.ThirdThem: editor.putBoolean("isThem", true); editor.putInt("Them", 3); editor.commit(); Intent intent3=new Intent(this,MainActivity.class); startActivity(intent3); break; default: break; } } }
styles.xml
<resources> <!-- Base application theme, dependent on API level. This theme is replaced by AppBaseTheme from res/values-vXX/styles.xml on newer devices. --> <style name="AppBaseTheme" parent="Theme.AppCompat.Light"> <!-- Theme customizations available in newer API levels can go in res/values-vXX/styles.xml, while customizations related to backward-compatibility can go here. --> </style> <!-- Application theme. --> <style name="AppTheme" parent="AppBaseTheme"> <!-- All customizations that are NOT specific to a particular API-level can go here. --> </style> <style name="FirstThem"> <item name="android:textColor">@color/FirstThemTextColor</item> <!-- 字體顏色 --> <item name="android:windowBackground">@color/FirstThemBackgroundColor</item> <!-- 窗口背景 --> </style> <style name="SecondThem"> <item name="android:textColor">@color/SecondThemTextColor</item> <!-- 字體顏色 --> <item name="android:windowBackground">@color/SecondThemBackgroundColor</item> <!-- 窗口背景 --> </style> <style name="ThirdThem"> <item name="android:textColor">@color/ThirdThemTextColor</item> <!-- 字體顏色 --> <item name="android:windowBackground">@color/ThirdThemBackgroundColor</item> <!-- 窗口背景 --> </style> </resources>
colors.xml
<?xml version="1.0" encoding="utf-8"?> <resources> <color name="FirstThemTextColor">#000000</color> <color name="FirstThemBackgroundColor">#FFFFFF</color> <color name="SecondThemTextColor">#AAAAAA</color> <color name="SecondThemBackgroundColor">#EEBBEE</color> <color name="ThirdThemTextColor">#CCCCCC</color> <color name="ThirdThemBackgroundColor">#AAAADD</color> </resources>