初识Style和Theme
初识Style和Theme
学习自
认识Style
大家还记得如何设置一个 无限循环的
或者 具有具体进度的
ProgressBar吗?
<ProgressBar
style="@style/Base.Widget.AppCompat.ProgressBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp" />
显示效果
<ProgressBar
style="@style/Base.Widget.AppCompat.ProgressBar.Horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:progress="50" />
显示效果
那为什么同样都是一个 ProgressBar
控件,但是为什么会有如此大相庭径的显示效果呢?请注意这两种的显示效果的 Style
是不相同的。
我们看一下这两个Style的源码:
<style name="Base.Widget.AppCompat.ProgressBar" parent="android:Widget.ProgressBar">
<item name="android:minWidth">@dimen/abc_action_bar_progress_bar_size</item>
<item name="android:maxWidth">@dimen/abc_action_bar_progress_bar_size</item>
<item name="android:minHeight">@dimen/abc_action_bar_progress_bar_size</item>
<item name="android:maxHeight">@dimen/abc_action_bar_progress_bar_size</item>
</style>
<!-- Base.Widget.AppCompat.ProgressBar 继承自 android:Widget.ProgressBar-->
<!-- android:Widget.ProgressBar 的源码-->
<style name="Widget.ProgressBar">
<item name="indeterminateOnly">true</item>
<item name="indeterminateDrawable">@drawable/progress_medium_white</item>
<item name="indeterminateBehavior">repeat</item>
<item name="indeterminateDuration">3500</item>
<item name="minWidth">48dip</item>
<item name="maxWidth">48dip</item>
<item name="minHeight">48dip</item>
<item name="maxHeight">48dip</item>
<item name="mirrorForRtl">false</item>
</style>
从上面的Style中我们可以发现,这不就是View的属性吗。看到这里,我们应该就对Style有一个了解了——Style 就是对View的属性的抽取,避免了繁琐的设置属性的机械性工作,并且也更易于修改,避免了冗余的代码。
自定义Style
在一个Android项目中,使用统一的样式的Button是非常重要的,所以Button的一些通用的属性我们可以抽取成为 Style
。直接自 res/values/styles.xml
文件中添加。
<!--
声明一个Button的Style
一个粉色的背景
白色的字体
-->
<style name="MyButton">
<item name="android:background">#c25454</item>
<item name="android:textAllCaps">false</item>
<item name="android:textColor">#FFFFFF</item>
<item name="android:layout_width">wrap_content</item>
<item name="android:layout_height">wrap_content</item>
</style>
在布局文件中使用它。
<!-- 通过使用style,避免了繁琐的属性的声明 -->
<Button
style="@style/MyButton"
android:text="Button" />
Style的继承
Style同样是可以进行继承的。
<style name="MyButton">
<item name="android:background">#c25454</item>
<item name="android:textAllCaps">false</item>
<item name="android:textColor">#FFFFFF</item>
<item name="android:layout_width">wrap_content</item>
<item name="android:layout_height">wrap_content</item>
</style>
<!--其他属性继承自MyButton,但是自己又重新定义了background的值-->
<style name="MyButton.Blue" parent="MyButton">
<item name="android:background">#90206dcc</item>
</style>
Theme
Style是为View设置的,那么有没有这么一种“Style”是为一个Activity乃至为整个系统设置一个 Style
呢? 当然是有这么一个东西了,他就是 Theme
。
Theme可以应用于 Activity和Application(整个应用),但是为了保证真个APP的界面的风格是统一的,正常情况下并不会为Activity来单独地设置一个Theme,往往都是为整个Application来设置Theme。
为Application和Activity设置Theme
<!-- Theme属性设置整个Application的Theme -->
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<!-- Theme属性设置属于该Activity的Theme -->
<activity
android:name=".MainActivity"
android:theme="@style/Theme.AppCompat.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
@style/AppTheme
Application被设置的一个默认的Theme APPTheme
位于 src/values/styles.xml
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
可以看出来 Theme
也是一个Style,我们可以同改变AppTheme的相关属性来影响Application的显示效果。
Theme Editor
当然,通过写 xml
的代码来自定义个Theme真是太痛苦了。幸好AndroidStudio为我们提供了Theme的GUI的设计器。
总结
根据上面的选项我们就可以比较轻松的修改我们的Theme了,当然如果想要让Theme更好看一些,还是得需要参考一些设计的比较好的作品,或者说采用一些经典的配色,本文就到此结束了,在下一篇的文章呢,将会带学习一下Android换肤的功能。
初识Style和Theme的更多相关文章
- Android中Style和Theme的使用
Style: Style是View中一些属性的集合,包括height,padding,font color,background等等,Style单独定义在xml文件中,类似与web页面中css的角色, ...
- android UI进阶之style和theme的使用
今天来和大家分享一下android中UI设计里面常会用到的style和theme. 首先,style和theme都是资源,android提供了很多这样的默认资源.你可以来使用它们.同时你也可以自己定义 ...
- Android入门第十六篇之Style与Theme [转]
本文来自http://blog.csdn.net/hellogv/ ,引用必须注明出处! 越来越多互联网企业都在Android平台上部署其客户端,为了提升用户体验,这些客户端都做得布局合理而且美观.. ...
- Android中style和theme的区别
在学习Xamarin android的过程中,最先开始学习的还是熟练掌握android的六大布局-LinearLayout .RelativeLayout.TableLayout.FrameLayou ...
- 【转】说说Android中的style和theme
最近在做软件从2.3到4.0的改变的一些工作,其中涉及了一些style和theme相关的东西.上网上查了一些东西,这个一并说说.关于android中style和theme的基本使用,这里就不再赘述了, ...
- 【Android】attr、style和theme
一.Attr 属性,风格样式的最小单元: Attr 的定义 在自定义 View 的时候,在 res/attrs.xml 文件中声明属性,而Android 系统的属性也是以同样的方式定义的.比如 lay ...
- Android零碎知识之Style and Theme
Android的styles资源文件中存在了我们在应用中定义的各种style,它们都是以style开始的元素,包含许多属性的集合.但我们一般般它们分为style和theme,那它们有什么区别呢? 一. ...
- Android 中的style和Theme的使用
说明 style和theme的定义是为了改变原有系统设定的默认窗体.字体.背景色.格式等风格而使用.其本质就是系统属性的集合.本篇主要介绍android中的style和theme的具体用法. styl ...
- Android笔记(七十二) Style和Theme
我们尝尝需要使用setText.setColor.setTextSize等属性来设置控件的样式,但是每个控件都需要设置这些属性,工作量无疑是巨大的,并且后期维护起来也不方便. Style Androi ...
随机推荐
- CMake 实践教程
本篇博客是根据 <<CMake Practice>> 一文编写, 目的有三: 其一: 提取出其中的精要部分; 其二: 对其中不易理解的地方进行简要说明; 其三: 方便后续查找复 ...
- 首次使用Vue开发
1.首先在页面上添加如下的代码 var app = new Vue({ el: '#signupForm', data: { UserName: '', PWD: '' } }); 2.在下面添加ht ...
- 详解Jquery选择器
1.常见的选择器 id,类,标签选择器. $("#a1") $(".myclass") $("div") 2.组合选择器 $("# ...
- 【转】SSH服务详解
[转]SSH服务详解 第1章 SSH服务 1.1 SSH服务协议说明 SSH 是 Secure Shell Protocol 的简写,由 IETF 网络工作小组(Network Working Gro ...
- STM32F103X datasheet学习笔记---RCC(reset and clock control)
1.前言 本文主要记录stm32 关于reset 和 clock部分 datasheet的内容. 2.reset 有三种类型的reset:system reset, power reset, back ...
- ES系列十四、ES聚合分析(聚合分析简介、指标聚合、桶聚合)
一.聚合分析简介 1. ES聚合分析是什么? 聚合分析是数据库中重要的功能特性,完成对一个查询的数据集中数据的聚合计算,如:找出某字段(或计算表达式的结果)的最大值.最小值,计算和.平均值等.ES作为 ...
- Python-HTML CSS 练习
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- flexible array柔性数组、不定长的数据结构Struct详解
柔性数组,这个名词对我来说算是比较新颖的,在学习跳跃表的实现时看到的.这么好听的名字,的背后到底是如何的优雅. 柔性数组,其名称的独特和迷惑之处在于“柔性”这个词.在C/C++中定义数组,是一个定长的 ...
- XHR简介
在XHR诞生前,网页要获取客户端和服务器的任何状态更新,都需要刷新一次,在XHR诞生后就可以完全通过JS代码异步实现这一过程.XHR的诞生也使最初的网页制作转换为开发交互应用,拉开了WEB2.0的序幕 ...
- three.js 相机camera位置属性设置详解
开始很懵逼,完全不能理解,有个position,还要up和lookAt干嘛. [黑人问号脸❓❓❓] 既然是位置属性不明白,那默认其它属性都懂了. 上坐标轴: 先来第一个position属性,可以设置x ...