初识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. avloadingindicatorview 使用解析

    官方文档:https://github.com/81813780/AVLoadingIndicatorView 中文文档:https://www.helplib.com/GitHub/article_ ...

  2. Latex 公式颜色

    如何给这个公式加上颜色? 解决方法: \usepackage{xcolor} \begin{align}   \textcolor{red}{\int_a^b}\textcolor{blue}{f(x ...

  3. STM32F103X datasheet学习笔记---Interrupts and events

    1.前言 本章主要介绍STM32中断和事件相关的内容 2.NVIC NVIC管理着包括内核异常等中断 主要特性 68个外部中断源(不包含16个内部中断线) 可编程优先级为16级 低延迟异常和中断处理 ...

  4. input 子系统架构总结【转】

    Linux输入子系统(Input Subsystem) 转自:http://blog.csdn.net/lbmygf/article/details/7360084 Linux 的输入子系统不仅支持鼠 ...

  5. scn 时间

    Scn转换成时间: select to_char(scn_to_timestamp(3998591352171),'YYYY-MM-DD HH24:MI:SS') from dual: 时间转换成sc ...

  6. saltStack的event接口通过mysql数据库接收SaltStack批量管理日志

    event是一个本地的ZeroMQ PUB Interface,event是一个开放的系统,用于发送信息通知salt或其他的操作系统.每个event都有一个标签.事件标签允许快速制定过滤事件.除了标签 ...

  7. JS排序算法之快速排序

    const Arr = [85, 24, 63, 45, 17, 31, 96, 50]; function quickSort(arr) { 80 if (arr.length <= 1) { ...

  8. react之异步请求数据,render先行渲染报错,未拿到数据

    import React from 'react' import {connect} from 'react-redux' import { Redirect} from 'react-router- ...

  9. wpf 加阴影效果导致内容模糊的问题解决

    这个和GPU有关,参考地址 https://www.cplotts.com/2009/02/25/gpu-effects-blurry-text/ 产生问题的代码如下: <Window x:Cl ...

  10. TensorFlow 框架

    TensorFlow TensorFlow核心程序由2个独立部分组成:   a:Building the computational graph构建计算图   b:Running the comput ...