Android中theme.xml与style.xml的区别

一、相同点

两者的定义相同、继承方式也相同

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
<?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中:         

1
2
<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中。比如:

1
2
3
4
5
<EditText <br>    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的优先级高于主题

posted @   mingruqi  阅读(1395)  评论(0编辑  收藏  举报
编辑推荐:
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
阅读排行:
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· 单线程的Redis速度为什么快?
· 展开说说关于C#中ORM框架的用法!
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库
点击右上角即可分享
微信分享提示