Android Style和自定义属性

1. Android Style & Theme

1.1 基本概念

Styles and themes on Android allow you to separate the details of your app design from the UI structure and behavior, similar to stylesheets in web design.

Android上的style和theme允许你将应用中设计的详细细节与UI结构和行为进行分离,类似于web设计中的样式表。

1.1.1 Style(样式)

A style is a collection of attributes that specify the appearance for a single View. A style can specify attributes such as font color, font size, background color, and much more.

Style是一些可以指定单个View外观的属性集合,一个style可以指定一些属性比如字体颜色,字体大小,背景颜色等等。

1.1.2 Theme(主题)

A theme is a type of style that’s applied to an entire app, activity, or view hierarchy—not just an individual view. When you apply your style as a theme, every view in the app or activity applies each style attribute that it supports. Themes can also apply styles to non-view elements, such as the status bar and window background.

Theme是一种应用于整个app,activity或者view层次的style,而不仅仅是单独的view。当你应用你的style作为theme时,每一个app或者activity中的view将应用它支持的每个style属性。theme还可以将style应用于非view元素,例如状态栏和窗口背景。

Style和theme定义在res/values/文件夹下,通常命名为styles.xml(也可以根据需求命名为其他文件名)。

1.2 创建和应用style

打开res/values/styles.xml文件

  1. 添加一个以独特ID命名的<style>元素
  2. 为每个你想要定义的style属性添加<item>元素

举个栗子:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <style name="GreenText" parent="TextAppearance.AppCompat">
        <item name="android:textColor">#00FF00</item>
    </style>
</resources>

使用方法如下:

<TextView
    ...
    style="@style/GreenText"
    ... />

每个style中定义的view所支持的属性会被应用到view中。view会忽略掉它所不支持的属性。

注意:
只有添加style属性的元素才会接受这些属性,任何子view不会应用这些style。如果你想要子视图继承style,取而代之的是使用android:theme属性的style。

1.3 扩展和自定义style

为了保持与平台UI的兼容性,在创建自己style的时候,通常继承自framework或support库中已经存在的style。如果要继承style,需要在style中指定parent属性。之后,你可以覆盖继承style的属性并添加一个新的。
举个栗子:

<style name="GreenText" parent="@android:style/TextAppearance">
    <item name="android:textColor">#00FF00</item>
</style>

然而,通常应用的核心style是从Android Support库中继承的。support库中的style通过针对每个版本中可用的UI属性进行了优化,提供了与Android 4.0(API 14)和更高版本的兼容性。Support库中的style通常有一个与framework中的style相似的名称,但是包含“AppCompat”。例如:

<style name="GreenText" parent="TextAppearance.AppCompat">
    <item name="android:textColor">#00FF00</item>
</style>

你也可以通过点符号拓展style名称来继承style(平台的style除外),代替使用parent属性。也就是说,将你想要继承的style名称作为前缀,以点号分隔。当你继承自自己的style,而非其他库时,通常这么做。例如下面的例子,继承自GreenText并增加了文本大小。

<style name="GreenText.Large">
    <item name="android:textSize">22dp</item>
</style>

你可以不断继承style很多次只要你愿意链接更多的名字。例如为GreenText增加一个大写style

<style name="GreenText.Large.AllCaps">
    <item name="android:textAllCaps">true</item>
</style>

注意:
如果你使用点符号来拓展style,并且使用了parent属性,这时parent属性会覆盖掉所有你继承自点符号的属性。
例如定义一个BlueText style

<style name="BlueText" parent="TextAppearance.AppCompat">
    <item name="android:textColor">#0000FF</item>
</style>

修改GreenText.Large.AllCaps属性继承自BlueText

<style name="GreenText.Large.AllCaps" parent="BlueText">
    <item name="android:textAllCaps">true</item>
</style>

在View类文件的"XML attributes"表(例如TextView的"XML attributes"表)中,你可以找到以<item>标签定义的各种属性。所有的view都支持基类view中的"XML attributes",大部分view都可以添加自己特殊的属性。

1.4 创建和应用theme

创建theme的方法与创建style的方法相同,不同的地方在于如何应用。应用style是在view中定义一个style属性,应用theme是在AndroidManifest.xml中的<application>或者<activity>标签中添加android:theme属性。
例如,以下是将support库中采用材料设计的"dark"theme应用到整个app。

<manifest ... >
    <application android:theme="@style/Theme.AppCompat" ... >
    </application>
</manifest>

这段是将"Light"theme应用到activity上。

<manifest ... >
    <application ... >
        <activity android:theme="@style/Theme.AppCompat.Light" ... >
        </activity>
    </application>
</manifest>

现在app或者activity中的每个view都应用了给定theme的style。如果一个view仅支持style中定义的部分属性,它会应用这些属性并舍弃那些它不支持的。
从Android 5.0(API 21)和Android Support Library v22.1开始,你也可以在布局中给view定义android:theme属性。它会修改当前view及子view的theme,这对于改变界面特定部分的theme非常有用。
但通常我们会自定义一个theme应用到我们的app中。最好的方法是拓展support库中的style并覆写某些属性。我们稍后会讲到。

1.5 扩展和自定义theme

当你使用Android Studio创建一个工程的时候,它会对你的应用使用一个默认的材料设计theme,定义在了项目的styles.xml文件中。这个theme继承自Support库中的theme并且包含了被用作关键UI元素的颜色属性的覆盖,例如应用栏(app bar)和浮动动作按钮(floating action button)。
如下是style.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>

应该注意到,style值引用自其他颜色资源文件,定义在工程中res/values/colors.xml文件中,你可以通过修改这个文件来修改颜色。在开始修改颜色之前,可以使用Material Color Tool来预览颜色,这个工具可以帮助你从材料调色板中选择颜色并且预览它在app中的显示效果。
你可以覆盖theme中你想要修改的style属性,例如,你可以修改activity背景颜色:

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    ...
    <item name="android:windowBackground">@color/activityBackground</item>
</style>

你可以参考R.styleable.Theme来查看可以修改的theme属性。当对布局中的view添加style时,可以查看view类引用中的“XML attributes”表。在R.styleable.Theme中列出的一些theme属性适用于activity中的窗口,而非布局中的view。例如,windowBackground修改了窗口背景,windowEnterTransition定义了activity的入场动画。
Android Support Library也提供了其他属性,你可以使用它们自定义theme。
注意:
从Android Support Library中继承的theme修改属性时不需要加"android:"前缀。这个前缀只用于继承自Android framework的theme。

1.6 添加指定版本的style

如果你想要在Andorid新版本上添加theme属性。你可以将它们添加到你的theme中,并保持与旧版本的兼容性。你需要的是保存在其他包含资源版本限定符的values文件夹下的styles.xml文件。例如:

res/values/styles.xml        # themes for all versions
res/values-v21/styles.xml    # themes for API level 21+ only

由于values/styles.xml文件中的styles对全版本可见,所以定义在res/values-v21/styles.xml中的style可以继承自他们。你可以在style文件中定义一个base开头的theme并在特定版本的styles中拓展它,这样可以避免重复的style定义。
例如,在Android 5.0(API 21)或者更高版本发布了窗口转变的属性(windowActivityTransitions),如果你想要使用这个属性,你可以在res/values/styles.xml这样定义:

<resources>
    <!-- base set of styles that apply to all versions -->
    <style name="BaseAppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <item name="colorPrimary">@color/primaryColor</item>
        <item name="colorPrimaryDark">@color/primaryTextColor</item>
        <item name="colorAccent">@color/secondaryColor</item>
    </style>

    <!-- declare the theme name that's actually applied in the manifest file -->
    <style name="AppTheme" parent="BaseAppTheme" />
</resources>

在res/values-v21/styles.xml中这样定义:

&
  • 8
    点赞
  • 24
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值