- Android样式 (Style)
作用:android中的样式和CSS样式作用相似,都是用于为界面元素定义显示风格,其包含一个或者多个view控件属性的集合。如:需要定义字体的颜色和大小等
注意:Theme是用于整个Application和Activity级别的,而Sytle是用于界面中的控件的(控件级别)。
1)在控件中使用样式: style="@style/样式name"
2)定义样式: 样式定义位置res/values/styles.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<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>
</style>
</resources>
3)样式的继承: 样式和Java类类似,一个样式也可以继承另外的样式。
有两种继承方式:使用parent属性和前缀
<style name="CodeFont" parent="@android:style/TextAppearance.Medium"> //使用parent属性
//前缀方式来继承。使用的时候也要带上前缀
<style name="CodeFont.Red">
<item name="android:textColor">#FF0000</item>
</style>
<style name="CodeFont.Red.Big">
<item name="android:textSize">30sp</item>
</style>
- Android主题(Theme)
作用:主要是为了让应用中所有UI控件的样式保持一致,如所有界面的字体大小、颜色等
可以在Application和Activity中使用:
<application android:theme="@style/CustomTheme">
<activity android:theme="@android:style/Theme.Dialog">
常用系统主题:
android:style/Theme.Holo.Light 白底黑字
android:style/Theme.Holo 黑底白字
android:style/Theme.NoTitleBar.Fullscreen 无标题栏且全屏
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="Theme.Translucent : 透明背景
android:theme="Theme.Translucent.NoTitleBar" : 透明背景并无标题
android:theme="Theme.Translucent.NoTitleBar.Fullscreen" : 透明背景并无标题,全屏
android:theme="Theme.Panel ": 平板板风格显示
android:theme="Theme.Light.Panel" : 平板风格显示
Android5.0以上的主题用Theme.AppCompat为前缀的主题
android:theme="@style/Theme.AppCompat.Light"
例:
1.在style文件中定义如下style:
<color name="custom_theme_color">#b0b0ff</color>
<!-- 自定义主题, 父类是android:Theme.Light -->
<style name="CustomTheme" parent=