android 中theme.xml与style.xml使用总结

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


一、相同点


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

    

<?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">

                </EditText>


二、 在R.attr定义中以window开头的一些属性只对theme有效。


三、如果一个应用使用了theme,同时应用下的view也使用了style,那么当theme与样式style发生冲突时,style的优先级高于主题。






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值