您的位置:首页 > 移动开发 > Android开发

Android中style和theme的区别

2017-11-04 14:48 330 查看
在学习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

[html] view
plain copy

 print?

<?xml version="1.0" encoding="utf-8" ?>  

<resources>  

  <style name="testTheme">  

    <item name="android:background">#ff8c00</item>  

    <item name="android:typeface">serif</item>  

  </style><pre code_snippet_id="2046357" snippet_file_name="blog_20161213_2_1293174" name="code" class="html"></resources></pre>  

<pre></pre>  

<pre></pre>  

<pre></pre>  

<pre></pre>  

在MainActivity中调用: [Activity(Label = "Xamarin_Android_12_11", MainLauncher = true, Icon = "@drawable/icon",Theme = "@style/testTheme")]

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

[html] view
plain copy

 print?

<?xml version="1.0" encoding="utf-8" ?>  

<resources>  

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

<style parent="AppBaseTheme" name="AppTheme">   

  <item name="android:typeface">serif</item>  

  <item name="android:windowNoTitle">true</item>  

</style>  

</resources>  

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 : 平板风格显示


2.定义一个Style

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

[html] view
plain copy

 print?

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

调用很简单:

[html] view
plain copy

 print?

  

[html] view
plain copy

 print?

<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自身,或者继承自定义的样式。

继承自定义的样式

[html] view
plain copy

 print?

<style name="text">  

   <item name="android:typeface">monospace</item>  

 </style>  

 <style parent="text" name="text.title">  

   <item name="android:textSize">30sp</item>  

 </style>  

继承android自身的样式

[html] view
plain copy

 print?

<style name="Dialog.custom" parent="android:Theme.Dialog">    

       <item name="android:windowBackground">@drawable/dialog_bg</item>    

</style>  

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

转载文章
http://blog.csdn.net/kebi007/article/details/53576380
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: