1. 在res新建attrs.xml
<?xml version="1.0" encoding="utf-8"?> <resources> <attr name="mainBackground" format="color" /> <attr name="mainTextColor" format="reference|color" /> <attr name="btnColor" format="color" /> <attr name="nightMode" format="string" /> </resources>
2. 然后在styles.xml分两种
<resources xmlns:android="http://schemas.android.com/apk/res/android"> <!-- 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="android:Theme.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. --> <item name="mainBackground">#ffffff</item> <item name="mainTextColor">#000000</item> <item name="btnColor">#00ff00</item> <item name="nightMode">夜间模式</item> </style> <style name="NightTheme" parent="android:Theme.Black"> <item name="mainTextColor">#ffffff</item> <item name="mainBackground">#000000</item> <item name="btnColor">#0000ff</item> <item name="nightMode">日间模式</item> </style> <!-- Application theme. --> <style name="AppTheme" parent="AppBaseTheme"> <!-- All customizations that are NOT specific to a particular API-level can go here. --> </style> </resources>
3. 在layout的xml这样用:(就是加个?在前面)
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="?mainBackground" android:orientation="vertical">
<TextView android:id="@+id/pageNum" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="?mainTextColor" />
super.onCreate(savedInstanceState); this.setTheme(R.style.NightTheme); setContentView(R.layout.main);