android theme相關待整理

1.android theme相關講解 http://blog.csdn.net/sshhbb/article/details/7219838

         http://blog.csdn.net/notice520/article/details/6171632      

學習中碰到的問題:

1.application 層 android:theme="@style/AppTheme"  activity 層 theme有啥作用?

教訓在activity中間添加android:theme="@android:style/Theme 可以得到老版本的optionsMenu效果


首先,style和theme都是資源,android提供了很多這樣的默認資源。你可以來使用它們。同時你也可以自己定義style和theme。這非常的簡單,只需要在res/values/這個路徑裏面新建一個.xml文件,而且他的根節點必須是<resources>.對每一個style和theme,給<style>element增加一個全局唯一的名字,也可以選擇增加一個父類屬性,我們寫的style和theme就會繼承這個父類的屬性。style和theme的定義格式相同。不過style是針對view來說的,
比如TextView,EditText這些,而theme必須針對整個activity或者整個程序,你必須在AndroidManifest.xml中的<application>或者<activity>中定義。 
先來看看style,比如如下一段代碼:
<?xml version="1.0" encoding="utf-8"?>
<resources>    

<mce:style name="CodeFont" parent="@android:style/TextAppearance.Medium">

<!--

<item name="android:layout_width">fill_parent</item>        

<item name="android:layout_height">wrap_content</item>       

<item name="android:textColor">#00FF00</item>  

<item name="android:typeface">monospace</item>    

--></mce:style>

<style name="CodeFont" parent="@android:style/TextAppearance.Medium" mce_bogus="1">     

<item name="android:layout_width">fill_parent</item>  

<item name="android:layout_height">wrap_content</item>    

<item name="android:textColor">#00FF00</item>    

<item name="android:typeface">monospace</item>  

</style>

</resources>

可以看到這個style的名字爲CodeFont。 parent後面就是父類的style, CodeFont繼承這個父類的屬性。可以看到這個父類的style是android中默認的,你也可以繼承你自定義的style,這時候不需要再寫parent屬性,而是使用ContFont.red這樣的方式,而且你可以繼續繼承,寫成ContFont.red.small。接下來每一個item定義一個屬性。定義屬性的最好方法就是在api文檔裏找到這個view的xml屬性,比如在EditText中有InputType這個屬性,那麼在你的style裏面你就可以來定義它。
 
這樣一個style就寫好了。
使用也非常簡單,我們只要在寫我們的view時,加入style標籤就可以了,就像這樣

<TextView    style="@style/CodeFont" mce_style="@style/CodeFont"    android:text="@string/hello" />  
面講講主題,前面已經說了。主題需要在AndroidManifest.xml中註冊。如果你想整個程序都使用這個主題,你可以這樣寫

<application android:theme="@style/CustomTheme">  

如果你只需要在某個Activity中使用主題,那麼只要在Activity標籤中寫入android:theme=就可以了,android有很多好的默認主題,比如

<activity android:theme="@android:style/Theme.Dialog">  

這就會使你的整個Activity變成一個對話框形式,或者,如果你希望背景是透明的,可以這樣寫

<activity android:theme="@android:style/Theme.Translucent">  
 
同樣的我們也可以繼承父類theme,寫法和style一樣,就不贅述了。當然,和style一樣,你也可以自己定義一個theme,寫個例子

<?xml version="1.0" encoding="utf-8"?>
<resources> 

<mce: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>  

--></mce:style>

<style name="CustomTheme" mce_bogus="1">

 <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>

</resources>  
 
如果你要在java代碼中加載主題的話,只要用setTheme(R.style.CustomTheme)就可以了,不過記得一定要在初始化任何view之前,比如一定要放在我們常用的setContentView()之前。通常,我們不建議這麼做。
在寫程序佈局的時候,熟練的使用style和theme是非常必要和有益的


系統自帶theme

<span style="font-size:16px;">

•android:theme="@android:style/Theme.Dialog"   將一個Activity顯示爲對話框模式

•android:theme="@android:style/Theme.NoTitleBar"  不顯示應用程序標題欄

•android:theme="@android:style/Theme.NoTitleBar.Fullscreen"  不顯示應用程序標題欄,並全屏

•android:theme="@android:style/Theme.Light"  背景爲白色

•android:theme="@android:style/Theme.Light.NoTitleBar"  白色背景並無標題欄 

•android:theme="@android:style/Theme.Light.NoTitleBar.Fullscreen"  白色背景,無標題欄,全屏

•android:theme="@android:style/Theme.Black"  背景黑色

•android:theme="@android:style/Theme.Black.NoTitleBar"  黑色背景並無標題欄

•android:theme="@android:style/Theme.Black.NoTitleBar.Fullscreen"    黑色背景,無標題欄,全屏

•android:theme="@android:style/Theme.Wallpaper"  用系統桌面爲應用程序背景

•android:theme="@android:style/Theme.Wallpaper.NoTitleBar"  用系統桌面爲應用程序背景,且無標題欄

•android:theme="@android:style/Theme.Wallpaper.NoTitleBar.Fullscreen"  用系統桌面爲應用程序背景,無標題欄,全屏

•android:theme="@android:style/Translucent" 半透明效果

•android:theme="@android:style/Theme.Translucent.NoTitleBar"  半透明並無標題欄

•android:theme="@android:style/Theme.Translucent.NoTitleBar.Fullscreen"  半透明效果,無標題欄,全屏

•android:theme="@android:style/Theme.Panel"

•android:theme="@android:style/Theme.Light.Panel"

</span>

具體展示見 http://stephen830.iteye.com/blog/1129203

    http://android.blog.51cto.com/268543/303728

疑問 ?android:textAppearanceMedium 前面?表示什麼意思?

發佈了42 篇原創文章 · 獲贊 3 · 訪問量 5萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章