
一、相同点
两者的定义相同、继承方式也相同
<?xml version="1.0" encoding="utf-8"?>
<resources> <!--
继承方式
1、parent
通过parent属性用来继承Android已经定义好的style
比如:parent="android:Theme.Dialog" 或 parent="@android:style/Theme.Dialog"
2、继承自定义的style,可以使用parent或 . 的方式
比如 <style name="parent.child"> View中调用时 style="@style/parent.child"
或 <style name="child" parent="@style/parent.child"> View中调用时 style="@style/child"
或 <style name="child" parent="parent.child"> View中调用时 style="@style/child"
-->
<style name="myDialog" parent="android:Theme.Dialog">
<item name="android:windowBackground">@mipmap/ic_launcher</item>
</style> <style name="parent">
<item name="android:textSize">30sp</item>
<item name="android:text">parent</item>
</style>
<!-- .的继承方式 -->
<style name="parent.child">
<item name="android:text">child</item>
<item name="android:textColor">#ff00ff</item>
</style> <!-- parent的继承方式-->
<style name="child" parent="@style/parent.child">
<item name="android:text">childs</item>
</style>
</resources>
二、不同点
一)、使用的地方不同
1.Theme 是用来设置应用全局主题风格的,对整个应用或某个Activity存在影响。 5.0 上可以让你局部的调整设计风格;
1) AndroidManifest.xml中:
<application android:theme="@android:style/theme">
<activity android:theme="@android:style/theme">
2) 在Activity通过代码设置:
setTheme(R.style.theme);
注意必须在setContentView()之前设置才有效
2.Style 主要是用在View上的,当你在view上设置 style 的时候,LayoutInflater会读取该 style 的内容并在任意单独设置的样式之前把该内容设置到 AttributeSet中。比如:
<EditText
android:layout_height="wrap_content"
android:text="EditText"
style="@style/Title"
android:layout_width="fill_parent"
android:id="@+id/editText1"/>
二)、 在R.attr定义中以window开头的一些属性只对theme有效。
三)、如果一个应用使用了theme,同时应用下的view也使用了style,那么当theme与样式style发生冲突时,style的优先级高于主题