在Web開發中,Html負責內容,CSS負責表現。同樣,在Android開發中,可以使用Theme、Style+UI組件的方式實現內容和形式的分離。
Style是針對窗體元素級別的,改變指定控件或者Layout的樣式
Theme是針對窗體級別的,改變窗體樣式;
style演示
定義一個styles,在res/values/目錄下建立styles.xml:
<style name="mystyle" parent="AppBaseTheme"> <item name="android:textSize">18sp</item> <item name="android:textColor">#ff0000</item> </style>
<item name="屬性">屬性值</item>
1、在布局文件中引用style
<TextView style="@style/mystyle" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/hello_world" />
2、在程序中設置style
text.setTextAppearance(this, R.style.mystyle);
效果:
Theme演示
1、在manifest當中設置主題
theme的style可以定義在styles.xml中,也可以單獨定義在自己新建的themes.xml:
<?xml version="1.0" encoding="utf-8"?> <resources xmlns:android="http://schemas.android.com/apk/res/android"> <style name="myTheme" parent="AppBaseTheme"> <item name="android:windowNoTitle">true</item> <item name="android:windowFullscreen">?android:windowNoTitle</item> </style> </resources>
如果整個工程用一個主題就在application 標簽中定義
android:theme="@style/myTheme"
其他屬性:可以在sdk查看:..\sdk\platforms\android-7\data\res\values\themes.xml
android:windowBackground
android:windowIsFloating
android:windowIsTranslucent
android:windowContentOverlay
android:windowAnimationStyle
android:backgroundDimEnabled
相關: