Android 的style和theme

例子

<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>
<item name="android:typeface">monospace</item>
</style>
</resources>

<TextView
style="@style/CodeFont"
android:text="@string/hello" />

style作用

  • 设计与内容分开
  • 可继承
  • 便于统一风格

书写方式

  • 关于继承系统的书写
<style name="CodeFont" parent="@android:style/TextAppearance.Medium">
...
</style>
  • 关于继承自定义的书写
<style name="CodeFont1" parent="CodeFont">
...
</style>

  • 关于继承系统theme的书写
<style name="LightThemeSelector" parent="android:Theme.Holo.Light">
...
</style>
<style name="LightThemeSelector" parent="@android:style/Theme.Holo.Light">
...
</style>
  • 关于继承自定义theme的书写
<style name="LightThemeSelector" parent="@style/Theme.AppCompat">
...
</style>

style与theme的区别

Theme是针对窗体级别的,改变窗体样式;
Style是针对窗体元素级别的,改变指定控件或者Layout的样式

简单的说就是Theme里面有包含了好多好多Style

android:theme与app:popupTheme

  • android:theme设置是View和子View的主题(API20+)
  • app:popupTheme设置的是该view节点下的view的theme(通俗的说就是类似css选择器的作用)

常用于Toolbar下

比如

<android.support.v7.widget.Toolbar  
    android:id="@+id/toolbar"  
    android:layout_width="match_parent"  
    android:layout_height="?attr/actionBarSize"  
    android:background="?attr/colorPrimary"  
    android:theme="@style/AppTheme.AppBarOverlay"  
    app:popupTheme="@style/AppTheme.PopupOverlay"/>  

android:theme="@style/AppTheme.AppBarOverlay设置了Toolbar的主题
app:popupTheme="@style/AppTheme.PopupOverlay"设置了Toolbar节点下的view的主题

默认情况下我们使用theme为android:Theme.Holo.Light.DarkActionBar那么ActionBar文字是白的,ActionBar Overflow弹出的是黑底白字,如果需求是白底黑字那么只要设置toolbar的app:popupTheme="ThemeOverlay.AppCompat.Light"


常用的自带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=”Theme.Translucent : 透明背景 
android:theme=”Theme.Translucent.NoTitleBar” : 透明背景并无标题 
android:theme=”Theme.Translucent.NoTitleBar.Fullscreen” : 透明背景并无标题,全屏 
android:theme=”Theme.Panel “: 面板风格显示 
android:theme=”Theme.Light.Panel” : 平板风格显示

常用的Theme的属性

名称 作用
android:windowIsTranslucent 设置透明属性(防止启动时候的闪屏)
android:windowBackground 设置背景图片
android:windowAnimationStyle Activity进入退出动画
android:windowNoTitle 不显示标题栏
android:textColor 默认字体颜色
android:windowFullscreen 是否全屏
android:windowIsFloating 是否浮现在activity之上
android:backgroundDimEnabled 背景是否模糊显示

常用的style元素属性

非官方

附上一张官方的

官方

装载自关于Android的style和theme

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 176,576评论 25 709
  • 1.Style和Theme Android的style分为两个方面: 1.Theme 是针对窗体级别的,改变窗体样...
    小飞猪阅读 7,514评论 0 0
  • ¥开启¥ 【iAPP实现进入界面执行逐一显】 〖2017-08-25 15:22:14〗 《//首先开一个线程,因...
    小菜c阅读 11,909评论 0 17
  • 转载请说明出处:Android开发细节--查漏补缺(一):UI篇 引言:一开始,先和大家可能从最开始接触Andro...
    androidjp阅读 4,791评论 0 9
  • 头顶蓝天 脚踩大地 八百米跳下 丝毫无惧 一身傲骨 浑然正气 舍身为国 青春散尽 这个英雄 谁可代替
    墨泼阅读 3,682评论 0 1

友情链接更多精彩内容