Android總結之style(樣式)和Theme(主題)

style(樣式)
style(樣式)是針對窗體元素級別的,改變指定控件或者Layout的樣式。 
抽取一些共同的屬性寫到style,能夠省略大量重複的屬性代碼。android

建立步驟:app

在res/values目錄下新建一個名叫style.xml的文件。增長一個 <resources>根節點。
在<resources>根節點中新建< style>元素,並設置一個惟一的name,也能夠選擇增長一個父類屬性(parent)。經過name來引用,而父類屬性標識了當前風格是繼承於哪一個風格。 (能夠引用另外一個style,但在通常狀況下,會繼承Android的標準風格資源並加以修改)
在<style>元素內部,能夠申明一個或者多個<item>,每個<item>定義了一個名字屬性,而且在元素內部定義了這個風格的值。
你能夠在其餘XML定義的資源中經過style=」@style/YourCustomStyle」引用。
//若是style中定義的屬性和View中定義的重複,View中的屬性會覆蓋style中定義的屬性。佈局

     <style name="MyStyle" parent="TextViewStyle">     <!--設置name 和parent-->
        <item name="android:textColor">#000</item>     <!--字體顏色-->
        <item name="android:textSize">20sp</item>      <!--字體大小-->
        <item name="android:layout_height">wrap_content</item>    <!--控件高度->
        <item name="android:layout_width">match_parent</item>     <!--控件寬度-->
        <item name="android:background">#8f00</item>              <!--背景色-->
        <item name="android:gravity">center_horizontal</item>     <!--view中內容的位置限定-->
        <item name="android:layout_weight">1</item>               <!--比重-->
    </style>字體

//此處沒有設置Parent屬性,但因爲name以MyStyle.開頭,因此MyStyle.red會徹底繼承MyStyle
    <style name="MyStyle.red" >  
        <item name="android:textColor">#000</item>              <!--字體顏色-->
        <item name="android:textSize">20sp</item>               <!--字體大小-->
        <item name="android:layout_height">wrap_content</item>  <!--控件高度-->
        <item name="android:layout_width">match_parent</item>   <!--控件寬度-->
        <item name="android:background">#8f00</item>            <!--背景色-->
        <item name="android:gravity">center_horizontal</item>   <!--view中內容的位置限定-->
        <item name="android:layout_weight">1</item>             <!--比重-->
    </style>spa

 //同上,MyStyle.red.big會徹底繼承MyStyle.red
    <style name="MyStyle.red.big" >  
        <item name="android:textColor">#000</item>               <!--字體顏色-->
        <item name="android:textSize">20sp</item>                <!--字體大小-->
        <item name="android:layout_height">wrap_content</item>   <!--控件高度-->
        <item name="android:layout_width">match_parent</item>    <!--控件寬度-->
        <item name="android:background">#8f00</item>             <!--背景色-->
        <item name="android:gravity">center_horizontal</item>    <!--view中內容的位置限定-->
        <item name="android:layout_weight">1</item>              <!--比重-->
    </style> xml


    注意:這種經過將名稱連接起來的繼承方法只適用於由您本身的資源定義的樣式。
    您沒法經過這種方法繼承 Android 內建樣式。
    要引用內建樣式(例如TextAppearance),您必須使用 parent 屬性。

Theme(主題)
Theme(主題)是針對窗體級別的,改變窗體樣式,對整個應用或某個Activity存在全局性影響。 
主題依然在<style>元素裏邊申明,也是以一樣的方式引用。不一樣的是Theme能夠在Android Manifest中定義的<application>和<activity>中經過設置屬性 android:theme=」@style/**「添加到整個程序或者某個Activity。 
注:主題是不能應用在某一個單獨的View裏。繼承

 <style name="CustomTheme">
 <item name="android:windowNoTitle">true</item>
 <item name="windowFrame">@drawable/screen_frame</item>
 <item name="windowBackground">@drawable/screen_background_white</item>
 <item name="panelForegroundColor">#FF000000</item>
 <item name="panelBackgroundColor">#FFFFFFFF</item>
 <item name="panelTextColor">?panelForegroundColor</item>
 <item name="panelTextSize">14</item>
 <item name="menuItemTextColor">?panelTextColor</item>
 <item name="menuItemTextSize">?panelTextSize</item>
 </style>
 咱們用了@符號和?符號來應用資源。
 @符號 代表了咱們應用的資源是已經定義過並存在的,能夠直接引用。(和佈局文件引用相同)
 ?符號 代表了咱們引用的資源的值在當前的主題當中定義過。經過引用在<item>裏邊定義的名字能夠作到
 (panelTextColor用的顏色和panelForegroundColor中定義的同樣)。資源


若是想設置某個主題,但又想作少許調整以知足需求,只需將該主題添加爲當前主題的parent便可。例如:it

        // 在Theme.AppCompat.Dialog主題的基礎上加以修改
<style name="customDialog" parent="Theme.AppCompat.Dialog">
        <item name="android:windowFrame">@null</item>               <!--取消默認Dialog的windowFrame框-->
        <item name="android:windowNoTitle">true</item>              <!--設置無標題Dialog-->
        <item name="android:backgroundDimEnabled">true</item>       <!--是否四周變暗-->
        <item name="android:windowIsFloating">true</item>           <!-- 是否懸浮在activity上 -->
        <item name="android:windowContentOverlay">@null</item>      <!--取消默認ContentOverlay背景 -->
        <item name="android:windowBackground">@android:color/transparent</item> <!--取消window默認背景 否則四角會有黑影-->

注:若是一個應用使用了theme,同時應用下的view也使用了style,那麼當theme與樣式style發生衝突時,style的優先級高於主題。io

系統自帶的經常使用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」 用系統桌面爲應用程序背景,無標題欄,全屏 android:theme=」Translucent」 半透明 android:theme=」Theme.Translucent.NoTitleBar」 半透明,無標題,全屏 android:theme=」Theme.Translucent.NoTitleBar.Fullscreen」 半透明,無標題,全屏 android:theme=」Theme.Panel」 半透明,無標題,全屏 android:theme=」Theme.Light.Panel」平板風格顯示  

相關文章
相關標籤/搜索