標籤:android 主題 樣式 theme
項目中經常使用style和Theme,但卻從來沒有考慮過它們的區別,只會copy來copy去的,有時候還有些迷茫,為了徹底告別迷茫,現把這兩者的區別和使用總結出來,供自己和大夥參考
一.範圍
Theme是針對表單層級的,改變表單樣式。
Style是針對表單元素層級的,改變指定控制項或者Layout的樣式
二.使用方式
Theme
1. 在res\values\ 下建立themes.xml或者styles.xml檔案
2. 添加<resouces>節點(根節點)
3. 添加自訂的style
4.(1)在AndroidManifest.xml檔案中,為Activity指定theme屬性(推薦)
(2)Activity建立時調用setTheme函數 (必須在setContentView前調用 )
<style name="MyBubbleTheme" parent="@android:style/Theme.Dialog" ><item name="android:windowBackground">@drawable/bubble</item></style>
<activity android:name=".BlurThemeActivity" android:theme="@style/MyBubbleTheme" />
5.系統內建的Theme
android:theme="@android:style/Theme.Dialog" //將一個Activity顯示為能話框模式android:theme="@android:style/Theme.NoTitleBar" //不顯示應用程式標題欄android:theme="@android:style/Theme.NoTitleBar.Fullscreen" //不顯示應用程式標題欄,並全屏android:theme="@Theme.Light" //背景為白色android:theme="Theme.Light.NoTitleBar" //白色背景並無標題列 android:theme="Theme.Light.NoTitleBar.Fullscreen" //白色背景,無標題列,全屏android:theme="Theme.Black" //背景黑色android:theme="Theme.Black.NoTitleBar" //黑色背景並無標題列android:theme="Theme.Black.NoTitleBar.Fullscreen" //黑色背景,無標題列,全屏android:theme="Theme.Wallpaper" //用系統案頭為應用程式背景android:theme="Theme.Wallpaper.NoTitleBar" //用系統案頭為應用程式背景,且無標題列android:theme="Theme.Wallpaper.NoTitleBar.Fullscreen" //用系統案頭為應用程式背景,無標題列,全屏
6.常用屬性
<item name="windowBackground">@android:drawable/screen_background_dark</item> <item name="windowFrame">@null</item> <item name="windowNoTitle">false</item> <item name="windowFullscreen">false</item> <item name="windowIsFloating">false</item> <item name="windowContentOverlay">@android:drawable/title_bar_shadow</item> <item name="windowTitleStyle">@android:style/WindowTitle</item> <item name="windowTitleSize">25dip</item> <item name="windowTitleBackgroundStyle">@android:style/WindowTitleBackground</item> <item name="android:windowAnimationStyle">@android:style/Animation.Activity</item>
style
1. 在res\values\ 下建立styles.xml檔案
2. 添加<resouces>節點(根節點)
3. 添加自訂的style
4.在特定控制項或layout中添加style屬性
<style name="MyTextStyle"> <item name="android:textColor">#FF0000C0</item> <item name="android:layout_width">wrap_content</item> <item name="android:layout_height">wrap_content</item> </style>
<TextView style="@style/MyTextStyle" />
樣本:
很多程式剛啟動的時候的Tips介面,氣泡視窗和毛半透明效果
代碼如下:
BlurThemeActivity.java
import android.app.Activity;import android.os.Bundle;import android.view.WindowManager;public class BlurThemeActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // Have the system blur any windows behind this one. getWindow().setFlags(WindowManager.LayoutParams.FLAG_BLUR_BEHIND, WindowManager.LayoutParams.FLAG_BLUR_BEHIND); }}
themes.xml
<style name="MyBubbleTheme" parent="@android:style/Theme.Dialog" > <item name="android:windowBackground">@drawable/bubble</item> <item name="android:windowNoTitle">true</item> </style>
AndroidManifest.xml
<activity android:name=".BlurThemeActivity" android:theme="@style/MyBubbleTheme" />