Android零碎知识之Style and Theme
Android的styles资源文件中存在了我们在应用中定义的各种style,它们都是以style开始的元素,包含许多属性的集合。但我们一般般它们分为style和theme,那它们有什么区别呢?
一、Style or Theme
what
theme是一种特殊的style,我们通常认为style是运用在一个view或者window上,而theme是运用在activity或者application上的。style在布局文件中通过style=@style/[style name]引入,注意这里没有名字空间前缀;theme通过android:theme=@style/[style name]。
有一个很重要的特性来区别使用style还是theme,通过style=@style/[style name]映入style元素的作用域仅仅是映入的view本身,不包含它的子view;但是通过android:theme映入的style元素会作用域它的子view。
其实也很好理解这个区别,style是“样式”的意思,它指单个元素的的样式;而theme是“主题”,主题是整体概念,所以它会影响元素及元素的子元素。
why
为什么需要style?style的目的有两个(个人总结,不一定全面):
- 提取公共属性项,减少代码量。
- 维护界面外观的整体性,使得应用界面风格的一致性。
二、Definition of style
theme和style的格式是相同的,不同的在于他们的作用对象和作用域不同。那么下面就来讲讲如何定义一个style
where
style必须定义在value目录下的styles文件中,该文件是xml格式,根元素是resource,在resource内部可以定义自己的style。style的定义位置如下:
<resources> <style name="AppTheme"
parent="Theme.AppCompat.Light">
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style> <style name="AppTheme.NoActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style> </resources>
how
style的定义中name的必须要写的,它表明了这个style的名字,也是使用该style时的唯一身份标签:R.style.[name]
接着是可选的parent字段,style允许类似于Java的基础机制,它可以继承parent什么的style的全部属性,然后自己覆盖原属性或者增加新属性,
很大程度上减少了代码量。继承的表示还有另外一种方式:
<!--继承自己写的style-->
<style name="AppTheme.NoActionBar">
...
</style>
<!--通用继承方式-->
<style name="AppTheme" parent="Theme.AppCompat.Light">
...
</style>
如上所示,第一种继承方式只适用于同一个文件中的多个style的继承关系,后一种可以继承来自android预定义的style和同文件中的style。
use
一个定义好的style,可以两种方式使用。
- 通过android:theme="@style/name"引入一个activity或者application或者其他需要元素;
- 通过布局文件中style="@style/[name]"引入。
最后style也可以通过后缀文件来适配不同的设备,这点就不提了。
Android零碎知识之Style and Theme的更多相关文章
- android UI进阶之style和theme的使用
今天来和大家分享一下android中UI设计里面常会用到的style和theme. 首先,style和theme都是资源,android提供了很多这样的默认资源.你可以来使用它们.同时你也可以自己定义 ...
- Android下Notification,样式style,主题theme的功能实现
一:Notification 1.NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVIC ...
- 【Android】attr、style和theme
一.Attr 属性,风格样式的最小单元: Attr 的定义 在自定义 View 的时候,在 res/attrs.xml 文件中声明属性,而Android 系统的属性也是以同样的方式定义的.比如 lay ...
- Android零碎知识(一)
public abstract Resources getResources () Return a Resources instance for your application's package ...
- Android零碎知识
1.当启动一个APP时按下后退键会调用onBackPressed方法. 如果想要屏蔽后退键只需要重写onBackPressed方法如下即可: @Override public void onBackP ...
- Android 样式和主题(style & theme)
Android 样式 android中的样式和CSS样式作用相似,都是用于为界面元素定义显示风格,它是一个包含一个或者多个view控件属性的集合.如:需要定义字体的颜色和大小. 在CSS中是这样定义的 ...
- android的style控制Theme
value-v14就是在API14(4.0)的手机上所使用的Theme(其他版本以此类推) theme的名字叫做AppTheme,后面写有继承自哪个Theme,如下所示 <style name= ...
- Android:Style和Theme
在Web开发中,Html负责内容,CSS负责表现.同样,在Android开发中,可以使用Theme.Style+UI组件的方式实现内容和形式的分离. Style是针对窗体元素级别的,改变指定控件或者L ...
- Android中Style和Theme的使用
Style: Style是View中一些属性的集合,包括height,padding,font color,background等等,Style单独定义在xml文件中,类似与web页面中css的角色, ...
随机推荐
- 【codeforces 750E】New Year and Old Subsequence
time limit per test3 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- 关于使用Timer定时监测网络是否ping通
项目需要连接某台具体服务端,如果连不上则实时提示,开始使用Timer实时检测 void timer_Tick(object sender, EventArgs e) { Ping pingSender ...
- 【codeforces 776D】The Door Problem
[题目链接]:http://codeforces.com/contest/776/problem/D [题意] 每个门严格由两个开关控制; 按一下开关,这个开关所控制的门都会改变状态; 问你能不能使所 ...
- 属性动画Property Animation
ViewPropertyAnimation 使用方式:View.animate() 后跟 translationX() 等方法,动画会自动执行. 注意translationX实现是调用对应的set方法 ...
- 【25.47%】【codeforces 733D】Kostya the Sculptor
time limit per test3 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- react中属性默认值是true?
看到项目代码中 return ( <MyWebView state="Login" title="登录app" ref="login" ...
- codeblocks如何设置,启动后自动打开上次未关闭的workspace
设置--环境--普通设置--on application start-up -------default 关闭code::blocks时不要关那个workspace就可以.至于其它方法就不知道了.
- js typeof instanceof
一般都是用typeof推断变量存在 例如if(typeof a!="undefined"){}.不是要去使用if(a)因为假定a不存在(未申报)将是错误的. 由于typeof经验n ...
- webpack优化经验1(持续)
1 不知道该优化哪里 先开启gzip压缩,这样可以很直接的减少请求包的体积,效果显著,不过需要在服务器端作相应的配置才能生效 2拆分vendor包, 减少单体包的体积,并行加载 通过配置,将不同的公用 ...
- 简单IO,将一段字符串存入一个记事本
using System; using System.IO; using System.Text; namespace 字符串存入记事本 { class Program { static void M ...