在android程序設計中我們可以更換主題(Theme),有兩種方法,一種是在主程序中使用setTheme()函數,傳入相應的參數,另一種是在AndroidManifest.xml中直接指定,這裏主要講第一種方法,也是最常用的。一般來說,對程序外觀屬性操作的函數都會在onCreate()調用之後被調用,但是這個setTheme()函數有點奇怪,如果,把它放在onCreate()之後調用,它就不會生效(但並不產生任何錯誤和警告),一定要放在onCreate()之前調用纔行.
代碼示例 :
public class ThemeSetActivity extends Activity {
/** Called when the activity is first created. */
@Override public void onCreate(Bundle savedInstanceState) {
setTheme(R.style.Transparent); // 自定義主題效果出現
super.onCreate(savedInstanceState);
// setTheme(R.style.Transparent); // 自定義主題效果不會出現
setContentView(R.layout.main); }}
style.xml
<style name="Transparent">
<item name="android:windowBackground">
@drawable/checkbox_pressed1
</item>
<item name="android:windowNoTitle">
true
</item>
</style>