初识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的更多相关文章

  1. Android中Style和Theme的使用

    Style: Style是View中一些属性的集合,包括height,padding,font color,background等等,Style单独定义在xml文件中,类似与web页面中css的角色, ...

  2. android UI进阶之style和theme的使用

    今天来和大家分享一下android中UI设计里面常会用到的style和theme. 首先,style和theme都是资源,android提供了很多这样的默认资源.你可以来使用它们.同时你也可以自己定义 ...

  3. Android入门第十六篇之Style与Theme [转]

    本文来自http://blog.csdn.net/hellogv/ ,引用必须注明出处! 越来越多互联网企业都在Android平台上部署其客户端,为了提升用户体验,这些客户端都做得布局合理而且美观.. ...

  4. Android中style和theme的区别

    在学习Xamarin android的过程中,最先开始学习的还是熟练掌握android的六大布局-LinearLayout .RelativeLayout.TableLayout.FrameLayou ...

  5. 【转】说说Android中的style和theme

    最近在做软件从2.3到4.0的改变的一些工作,其中涉及了一些style和theme相关的东西.上网上查了一些东西,这个一并说说.关于android中style和theme的基本使用,这里就不再赘述了, ...

  6. 【Android】attr、style和theme

    一.Attr 属性,风格样式的最小单元: Attr 的定义 在自定义 View 的时候,在 res/attrs.xml 文件中声明属性,而Android 系统的属性也是以同样的方式定义的.比如 lay ...

  7. Android零碎知识之Style and Theme

    Android的styles资源文件中存在了我们在应用中定义的各种style,它们都是以style开始的元素,包含许多属性的集合.但我们一般般它们分为style和theme,那它们有什么区别呢? 一. ...

  8. Android 中的style和Theme的使用

    说明 style和theme的定义是为了改变原有系统设定的默认窗体.字体.背景色.格式等风格而使用.其本质就是系统属性的集合.本篇主要介绍android中的style和theme的具体用法. styl ...

  9. Android笔记(七十二) Style和Theme

    我们尝尝需要使用setText.setColor.setTextSize等属性来设置控件的样式,但是每个控件都需要设置这些属性,工作量无疑是巨大的,并且后期维护起来也不方便. Style Androi ...

随机推荐

  1. java工程师之旅-一个月工作心得

    不知不觉,在工作中已经度过一个月,距离上次写文章已经好几个月了,正好还有二十分钟下班,抽点时间来写一下博文,写一下心得. 首先说一下,在我工作之前,做了一个项目,和一个外校大四的学生做一个毕业设计,一 ...

  2. 福利爬虫妹子图之获取种子url

    import os import uuid from lxml import html import aiofiles import logging from ruia import Spider, ...

  3. zabbix系列(三)zabbix3.0.4微信告警配置详解

    一.准备工作 申请微信公众号,并且是可以有发送消息的接口.添加有个脚本去调用微信的api. 之后可以参考下zabbix 的搭建,然后了解下脚本报警,之后再考虑报警方式的多样化. 个人微信一个 个人邮箱 ...

  4. as 插件GsonFormat用法(json字符串快速生成javabean)

    GsonFormat 主要用于使用Gson库将JSONObject格式的String 解析成实体,该插件可以加快开发进度,使用非常方便,效率高. 插件地址:https://plugins.jetbra ...

  5. C#面向对象(继承)

  6. JDBC辅助类封装 及应用

    一:代码图解: 二:配置文件: driverClassName=com.mysql.jdbc.Driver url=jdbc\:mysql\://127.0.0.1\:3306/xlzj_sh_new ...

  7. java多线程快速入门(七)

    什么是守护线程 守护线程是为用户线程服务的这么一个线程,主线程结束,守护线程也结束 package com.cppdy; class MyThread3 extends Thread{ @Overri ...

  8. LeetCode(28): 实现strStr()

    Easy! 题目描述: 实现 strStr() 函数. 给定一个 haystack 字符串和一个 needle 字符串,在 haystack 字符串中找出 needle 字符串出现的第一个位置 (从0 ...

  9. java.util.Random 类

    //: object/ForEachFloat.java package object; import java.util.Random; public class ForEachFloat { pu ...

  10. python 全栈开发,Day95(RESTful API介绍,基于Django实现RESTful API,DRF 序列化)

    昨日内容回顾 1. rest framework serializer(序列化)的简单使用 QuerySet([ obj, obj, obj]) --> JSON格式数据 0. 安装和导入: p ...