1. 程式人生 > >Android中activity背景色的設定

Android中activity背景色的設定

主題Theme就是用來設定介面UI風格,可以設定整個應用或者某個活動Activity的介面風格。在Android SDK中內建了下面的Theme,可以按標題欄Title Bar和狀態列Status Bar是否可見來分類:


[html] view plaincopyprint?
  1. android:theme="@android:style/Theme.Dialog"   將一個Activity顯示為能話框模式    
  2. android:theme="@android:style/Theme.NoTitleBar"  不顯示應用程式標題欄    
  3. android:theme=
    "@android:style/Theme.NoTitleBar.Fullscreen"  不顯示應用程式標題欄,並全屏    
  4. android:theme="Theme.Light"  背景為白色    
  5. android:theme="Theme.Light.NoTitleBar"  白色背景並無標題欄     
  6. android:theme="Theme.Light.NoTitleBar.Fullscreen"  白色背景,無標題欄,全屏    
  7. android:theme="Theme.Black"  背景黑色    
  8. android:theme="Theme.Black.NoTitleBar"
      黑色背景並無標題欄    
  9. android:theme="Theme.Black.NoTitleBar.Fullscreen"    黑色背景,無標題欄,全屏    
  10. android:theme="Theme.Wallpaper"  用系統桌面為應用程式背景    
  11. android:theme="Theme.Wallpaper.NoTitleBar"  用系統桌面為應用程式背景,且無標題欄    
  12. android:theme="Theme.Wallpaper.NoTitleBar.Fullscreen"用系統桌面為應用程式背景,無標題欄,全屏    
  13. android:theme=
    "Translucent"  半透明    
  14. android:theme="Theme.Translucent.NoTitleBar" 半透明、無標題欄    
  15. android:theme="Theme.Translucent.NoTitleBar.Fullscreen" 半透明、無標題欄、全屏    
  16. android:theme="Theme.Panel"
  17. android:theme="Theme.Light.Panel"

這些主題可以應用到整個應用Application範圍或者某個活動Activity範圍中。

應用Application範圍:在AndroidManifest.xml中的application節點中設定theme屬性,主題theme應用到整個應用程式中。

[html] view plaincopyprint?
  1. <application
  2.     Android:icon="@drawable/icon"  
  3.     Android:icon="@string/app_name"  
  4.     Android:icon="@android:style/Theme.Black.NoTitleBar">
活動Activity範圍:

使用java程式碼或者在AndroidManifest.xml中對活動Activity的主題進行設定,主題僅應用到當前活動中。在AndroidMainifest.xml設定方法:(XML中程式碼間不能有空格,注意中英文引號)

[html] view plaincopyprint?
  1. <activity
  2. android:name=".About"  
  3. android:label="@string/app_name"  
  4. android:theme="@android:style/Theme.Black.NoTitleBar" >

使用java程式碼進行設定,在當前活動Activity的onCreate中進行設定:

[java] view plaincopyprint?
@Override  
public void onCreate(Bundle savedInstanceState){  
super.onCreate(savedInstanceState);  
setTheme(android.R.style.Theme_Translucent_NoTitleBar);  
setContentView(R.layout.main);  
}  


背景顏色還可以在佈局XML中設定:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
	android:orientation="vertical" android:layout_width="fill_parent"
	android:layout_height="fill_parent"
	android:background="#ffffff">


直接設定Activity的背景顏色

一般我們都是使用佈局檔案直接載入到Activity,從而覆蓋了Activity的顏色。這裡沒有載入佈局檔案(沒有用R.layout.main)
import android.app.Activity;
import android.content.res.Resources;
import android.graphics.drawable.Drawable;
import android.os.Bundle;

public class Main extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Resources res = getResources();
        Drawable drawable = res.getDrawable(R.drawable.bkcolor);
        this.getWindow().setBackgroundDrawable(drawable);
    }
}

需要注意的是  需要在value目錄下的string.xml檔案中加入一段程式碼,如下
<drawable name="bkcolor">#ff00ff</drawable>