Android中style和theme的区别

转载自: https://blog.csdn.net/kebi007/article/details/53576380

在学习Xamarin android的过程中,最先开始学习的还是熟练掌握android的六大布局-LinearLayout 、RelativeLayout、TableLayout、FrameLayout、GridLayout、AbsoluteLayout。当你初步了解这六大布局之后,我极力要求大家熟练掌握android中style和theme的区别,这将会在android开发中让你的界面变得更加完美 。 这就像是在web后端程序的开发过程,掌握div+css极为重要。
 不说这么多废话了,听起来好像很麻烦的样子,其实不然,甚至过于简单化了!虽然没有大牛的技术,但是我还是希望把这样一篇简单的文章写得更容易理解一些。

1. Style和Theme的有哪些不同点和相同点

不同点:

  • Theme是应用于Activity或者是整个Application的,作用于单个Activity或者所有Acity,不能作用于某个控件的
  • Style是应用于某个(些)控件,Layout的,作用于控件级别的。
  • 两者总结一句就是:相对而言Theme是作用于全局的,而Style是作用于局部的。定义方式一样,使用的地方不一样。

相同点:

  • 都位于values文件夹下的style.xml中,定义的方法一样,都是控制UI的一堆属性。

注意:当一个Activity中的控件应用了Theme中的样式又应用了Style中的样式,那么Style中的样式优先于Theme。

2. 定义一个作用于MainActivity的Theme

<?xml version="1.0" encoding="utf-8" ?>
<resources>
  <style name="testTheme">
    <item name="android:background">#ff8c00</item>
    <item name="android:typeface">serif</item>
  </style>
</resources>

在MainActivity中调用:

<activity
    android:name=".MainActivity"
    android:icon="@drawable/icon"
    android:theme="@style/testTheme"
    android:label="Activity_title">
    <intent-filter>
        <action android:name="android.intent.action.MAIN"/>
        <category android:name="android.intent.category.LAUNCHER"/>
    </intent-filter>
</activity>

这是一个最简单的Theme,当然我们也可以使用android自身的主题,也可以继承android自身的主题。

<style name="AppBaseTheme" parent="android:Theme.Light">
</style>

<style name="AppTheme" parent="AppBaseTheme">
    <item name="android:typeface">serif</item>
    <item name="android:windowNoTitle">true</item>
</style>

parent是继承主题的意思,这个例子就是AppTheme主题继承了android自身的Theme.Light,设置字体为serif(android自带的其他两种字体是 sans,monospace)
其他的一些常用的android原生的Theme:
Theme.NoTitleBar : 不显示应用程序标题栏
Theme.NoTitleBar.Fullscreen : 不显示应用程序标题栏,并全屏

Theme.Light : 背景为白色
Theme.Light.NoTitleBar : 白色背景并无标题栏
Theme.Light.NoTitleBar.Fullscreen : 白色背景,无标题栏,全屏

Theme.Black : 背景黑色
Theme.Black.NoTitleBar : 黑色背景并无标题栏
Theme.Black.NoTitleBar.Fullscreen : 黑色背景,无标题栏,全屏

Theme.Wallpaper : 用系统桌面为应用程序背景
Theme.Wallpaper.NoTitleBar : 用系统桌面为应用程序背景,且无标题栏
Theme.Wallpaper.NoTitleBar.Fullscreen : 用系统桌面为应用程序背景,无标题栏,全屏

Theme.Translucent : 透明背景
Theme.Translucent.NoTitleBar : 透明背景并无标题
Theme.Translucent.NoTitleBar.Fullscreen : 透明背景并无标题,全屏

Theme.Panel : 面板风格显示
Theme.Light.Panel : 平板风格显示

3. 定义一个Style

定义style的方式还是和定义theme一样的,也是在style文件夹下面写属性

<style name="btnStyle">
    <item name="android:paddingTop">10dp</item>
    <item name="android:paddingBottom">10dp</item>
    <item name="android:background">#FF8C00</item>
    <item name="android:textSize">14sp</item>
    <item name="android:typeface">monospace</item>
</style>

调用很简单:

<Button  
      android:id="@+id/MyButton"  
      android:layout_width="match_parent"  
      android:layout_height="wrap_content"  
      style="@style/btnStyle"  
      android:text="@string/Hello" />  
那style是不是可以继承?答案是当然的,style和theme一样,也是可以继承android自身,或者继承自定义的样式。

继承自定义的样式:

<style name="text">
   <item name="android:typeface">monospace</item>
</style>
<style parent="text" name="text.title">
   <item name="android:textSize">30sp</item>
</style>

继承android自身的样式:

<style name="Dialog.custom" parent="android:Theme.Dialog">  
    <item name="android:windowBackground">@drawable/dialog_bg</item>  
</style>

总结:虽然style和theme的区别还是很好理解的,但是用起来还不是很顺手的,只有在实践中不断磨练,才能把style和theme合理利用好,才能把用户体验做的更好,忽然想起那句很牛逼的话,“以技术驱动业务发展”,如果界面做的好的话,也一定能给用户带来不一样的感受。

虽然简单,但有时候总结也是一种收获。

猜你喜欢

转载自blog.csdn.net/tgvincent/article/details/80936302