Android官方文档之User Interface(Styles and Themes)

User Interface(以下简称UI)是任何可以向用户展示、与用户交互的图形界面。Android提供了大量预定义的UI组件( a variety of pre-built UI components),比如Layout资源,除此之外,Android还提供了特殊的UI模型,如dialogs、notifications、menus 等。


从本文起,将介绍Android的各种UI资源以及如何自定义UI资源。本文将介绍:


您还可以参考这些博客文章:


或者这些Training:


样式和主题(Styles and Themes)


一个Style资源是一组属性的集合,用于指定一个View或一个Window的样子和格式(A style is a collection of properties that specify the look and format for a View or window. A style can specify properties ),比如高度、内边距、字体颜色、字体大小、背景颜色 等( such as height, padding, font color, font size, background color, and much more)。Style资源在XML资源中定义,以区分layout资源。


具有相同或相似外观的UI控件可以共享同一个Style资源,如下所示:

<TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:textColor="#00FF00"
    android:typeface="monospace"
    android:text="@string/hello" />

将上述TextView中某些属性抽取出来,定义在style资源中,并给该资源起名为CodeFont,这样其他TextView也可以应用该资源,这些TextView的风格将保持一致。

<TextView
    style="@style/CodeFont"
    android:text="@string/hello" />

theme 资源是一种特殊的style资源,只不过theme资源作用于整个Activity或Application上,而不是View或Window上(A theme is a style applied to an entire Activity or application, rather than an individual View)。将一个style作为theme资源作用在Activity或Application上,实际上是作用在了activity或application中的所有控件上。比如说,若将style资源CodeFont作为theme资源作用在了 Activity上,那么这个activity中的所有控件都将具有绿色(#00FF00)以及等宽字体(monospace font)。


定义样式(Defining Styles)


style应定义在res/values/目录下的XML文件中,文件名任意(The name of the XML file is arbitrary)。在文件中<resources>必须作为style资源的根标签。根标签内部包含您需要自定义的style资源,以<style>标签包含。其中<style>标签中的属性name是不可缺省的,它指定了该style资源的名字,如程序中需要引用该资源,那么引用的就是name中的内容。每一个<item>标签指定了style的一个属性,以下是一个自定义的style示例:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <style name="CodeFont" parent="@android:style/TextAppearance.Medium">
        <item name="android:layout_width">fill_parent</item>
        <item name="android:layout_height">wrap_content</item>
        <item name="android:textColor">#00FF00</item>
        <item name="android:typeface">monospace</item>
    </style>
</resources>

您可以在其他位置以@style/CodeFont的形式引用该style资源。<style>标签的parent 属性不是必须指定的,它的值是一个资源ID,引用了一个其他的style,这表示现在这个正在自定义的style继承了这个引用style的所有属性。您也可以在自己的style中覆盖需要替换的父style属性。


如果需要将一个style应用在一个activity或application上,那个这个style就变成了theme,它的定义与作用在view上的style没有任何区别( exactly the same as a style for a View),只是叫法不一样罢了。


继承(Inheritance)


通过<parent>属性,可以方便地继承一个现成的style资源,您可以在此基础上添加或修改某些属性。Android提供了大量的style资源,比如,系统提供了装饰字体的style资源:TextAppearance,您可以直接继承它,并修改它:

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

如果您需要继承自定义的style资源,那么无需使用<parent>属性。只需要在新定义的style资源的name属性中将被继承的style资源名作为前缀,并以” . “分隔即可:

 <style name="CodeFont.Red">
        <item name="android:textColor">#FF0000</item>
    </style>

例如,现在要继承自定义的CodeFont style,新的自定义style名字为Red,那么只需命名为:CodeFont.Red即可。并在其他位置以@style/CodeFont.Red的方式引用。


如果需要继续继承上述style,那么name也只需以链式的方式书写即可:

    <style name="CodeFont.Red.Big">
        <item name="android:textSize">30sp</item>
    </style>

!请注意:这种链式地style继承方式只适用于继承您自定义的style资源,如需继承Android自带的style资源,仍需使用<parent>属性。


样式属性(Style Properties)


<item>标签用于指定自定义的属性。您可以指定类似于layout_widthtextColor这样的属性。可以通过具体的View所具有的属性来定义。在代码中可以通过 R.attr来获取这些属性值。如果您引用的style中包含了不支持该控件的属性,那么这些属性将被忽略。


某些特殊的属性不被任何View支持,只能应用在theme中。也就是说,这些属性只被Activity或Application支持。比如有的属性用于确定是否隐藏应用的标题、是否隐藏状态栏或者修改window的背景色 等。这些属性并不属于任何View,它们有一个共同的特点,就是名字以window开头。比如:windowNoTitle 和 windowBackground 属性。


在UI中应用样式和主题(Applying Styles and Themes to the UI)


应用style的方式有两种:

  • 对于View来说,只需使用<style>标签引用style资源即可;

  • 对于Activity或Application来说,需要在 Android manifest文件中对应的<activity><application>标签中使用android:theme属性配置。


!请注意:将style资源应用在某个View上,它仅对该View的样式生效,也就是说,如果您将style资源应用在ViewGroup上,那么该资源不会自动地应用在ViewGroup的子View上,您只能将每一个View分别引用该style资源,或者干脆直接在ViewGroup的Activity中将该style作为theme引用。另外需要注意的是,style属性是不包含“android”前缀的


style应用在View上(Apply a style to a View)


示例如下:

<TextView
    style="@style/CodeFont"
    android:text="@string/hello" />

theme 应用在Activity 或 application上(Apply a theme to an Activity or application)


示例如下:(在AndroidManifest.xml 中)

<application android:theme="@style/CustomTheme">

如需将Activity定义成对话框的样子,那么可以引用Theme.Dialog主题:

<activity android:theme="@android:style/Theme.Dialog">

如需使Activity的背景透明,那么可以引用Theme.Translucent主题:

<activity android:theme="@android:style/Theme.Translucent">

如需定制系统主题,那么只需像style的继承那样定制即可:

<color name="custom_theme_color">#b0b0ff</color>
<style name="CustomTheme" parent="android:Theme.Light">
    <item name="android:windowBackground">@color/custom_theme_color</item>
    <item name="android:colorBackground">@color/custom_theme_color</item>
</style>

!请注意:由于android:windowBackground属性的值只支持引用类型,并不接受字面值,所以写法如上所示。


下面只需将该定制的主题应用到activity上即可:

<activity android:theme="@style/CustomTheme">

为整个应用选择一个基础主题(Select a theme based on platform version)


新的Android版本增加了一些系统主题,为了让这些主题也能应用在旧的Android版本中,应当在目录中使用不同后缀的文件夹做以区分,比如:android:Theme.Holo.Light是在Android 3.0之后新增的主题,那么您需要定制一个继承了该主题的theme,并把该主题放在res/values-v11/styles.xml中:

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

再在默认的目录中( res/values/styles.xml)定义如下主题:

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

因为android:Theme.Light主题在Android 3.0之前也支持,所以当设备版本小于3.0时,将加载该主题。


通过R.styleable.Theme可获取该主题的所有属性集合。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Pro Android 5 shows you how to build real-world and fun mobile apps using the Android 5 SDK. This book updates the best-selling Pro Android and covers everything from the fundamentals of building apps for smartphones, tablets, and embedded devices to advanced concepts such as custom components, multi-tasking, sensors/augmented reality, better accessories support and much more. Using the tutorials and expert advice, you'll quickly be able to build cool mobile apps and run them on dozens of Android-based smartphones. You'll explore and use the Android APIs, including those for media and sensors. And you'll check out what's new in Android, including the improved user interface across all Android platforms, integration with services, and more. By reading this definitive tutorial and reference, you'll gain the knowledge and experience to create stunning, cutting-edge Android apps that can make you money, while keeping you agile enough to respond to changes in the future. What you’ll learn How to use Android to build Java-based mobile apps for Android smartphones and tablets How to build irresistible user interfaces (UIs) and user experiences (UXs) across Android devices How to populate your application with data from data sources, using Content Providers How to build multimedia and game apps using Android's media APIs How to use Android's location-based services, network-based services, and security How to use key Android features, such as Fragments and the ActionBar Who this book is for This book is for professional software engineers and programmers looking to move their ideas and applications into the mobile space with Android. It assumes a passable understanding of Java, including how to write classes and handle basic inheritance structures. Table of Contents Chapter 1 Hello, World Chapter 2 Introduction to Android Applications Chapter 3 Basic User Interface Controls Chapter 4 Adapters and List Controls Chapter 5 Making Advanced UI Layouts Chapter 6 Adding Menus and ActionBar Chapter 7 Styles and Themes Chapter 8 Fragments Chapter 9 Responding to Configuration Changes Chapter 10 Dialogs: Regular and Fragment Chapter 11 Working with Preferences and Saving State Chapter 12 Compatibility Library Chapter 13 Exploring Packages, Processes, Components, Threads and Handlers Chapter 14 Working with Services Chapter 15 Advanced Async Task & Progress Dialogs Chapter 16 Exploring Broadcast Receivers and Long Running Services Chapter 17 Exploring the Alarm Manager Chapter 18 Unveiling 2D Animation Chapter 19 Exploring Maps and Location Services Chapter 20 Understanding the Media Frameworks Chapter 21 Home Screen Widgets Chapter 22 Touchscreens Chapter 23 Drag and Drop Chapter 24 Using Sensors Chapter 25 Understanding Content Providers Chapter 26 Understanding the Contacts API Chapter 27 Loaders Chapter 28 Security and Permissions Chapter 29 Google Cloud messaging and services Chapter 30 Deploying Your Application: Google Play Store and Beyond
Pro Android 5 shows you how to build real-world and fun mobile apps using the Android 5 SDK. This book updates the best-selling Pro Android and covers everything from the fundamentals of building apps for smartphones, tablets, and embedded devices to advanced concepts such as custom components, multi-tasking, sensors/augmented reality, better accessories support and much more. Using the tutorials and expert advice, you'll quickly be able to build cool mobile apps and run them on dozens of Android-based smartphones. You'll explore and use the Android APIs, including those for media and sensors. And you'll check out what's new in Android, including the improved user interface across all Android platforms, integration with services, and more. By reading this definitive tutorial and reference, you'll gain the knowledge and experience to create stunning, cutting-edge Android apps that can make you money, while keeping you agile enough to respond to changes in the future. What you’ll learn How to use Android to build Java-based mobile apps for Android smartphones and tablets How to build irresistible user interfaces (UIs) and user experiences (UXs) across Android devices How to populate your application with data from data sources, using Content Providers How to build multimedia and game apps using Android's media APIs How to use Android's location-based services, network-based services, and security How to use key Android features, such as Fragments and the ActionBar Who this book is for This book is for professional software engineers and programmers looking to move their ideas and applications into the mobile space with Android. It assumes a passable understanding of Java, including how to write classes and handle basic inheritance structures. Table of Contents Chapter 1 Hello, World Chapter 2 Introduction to Android Applications Chapter 3 Basic User Interface Controls Chapter 4 Adapters and List Controls Chapter 5 Making Advanced UI Layouts Chapter 6 Adding Menus and ActionBar Chapter 7 Styles and Themes Chapter 8 Fragments Chapter 9 Responding to Configuration Changes Chapter 10 Dialogs: Regular and Fragment Chapter 11 Working with Preferences and Saving State Chapter 12 Compatibility Library Chapter 13 Exploring Packages, Processes, Components, Threads and Handlers Chapter 14 Working with Services Chapter 15 Advanced Async Task & Progress Dialogs Chapter 16 Exploring Broadcast Receivers and Long Running Services Chapter 17 Exploring the Alarm Manager Chapter 18 Unveiling 2D Animation Chapter 19 Exploring Maps and Location Services Chapter 20 Understanding the Media Frameworks Chapter 21 Home Screen Widgets Chapter 22 Touchscreens Chapter 23 Drag and Drop Chapter 24 Using Sensors Chapter 25 Understanding Content Providers Chapter 26 Understanding the Contacts API Chapter 27 Loaders Chapter 28 Security and Permissions Chapter 29 Google Cloud messaging and services Chapter 30 Deploying Your Application: Google Play Store and Beyond
目录 应用程序基础Application Fundamentals............................................................... 4 关键类 ...................................................................................................................... 4 应用程序组件 ........................................................................................................... 5 激活组件:intent ............................................................................................. 7 关闭组件 ........................................................................................................... 8 manifest文件 .................................................................................................. 8 Intent过滤器................................................................................................... 9 Activity和任务 ...................................................................................................... 10 Affinity(吸引力)和新任务 ........................................................................... 11 加载模式 ......................................................................................................... 12 清理堆栈 ......................................................................................................... 14 启动任务 ......................................................................................................... 15 进程和线程 ............................................................................................................. 15 进程 ................................................................................................................ 15 线程 ................................................................................................................ 16 远程过程调用 .................................................................................................. 16 线程安全方法 .................................................................................................. 17 组件生命周期 ......................................................................................................... 18 Activity生命周期 ........................................................................................... 18 调用父类 ................................................................................................................ 19 服务生命周期 .................................................................................................. 22 广播接收器生命周期 ....................................................................................... 23 进程与生命周期 .............................................................................................. 24 用户界面User Interface ........................................................................................ 25 视图层次View Hierarchy ...................................................................................... 25 布局Layout ........................................................................................................... 26 部件Widgets ......................................................................................................... 27 用户界面事件UI Events ........................................................................................ 27 菜单Menus ........................................................................................................... 28 高级话题Advanced Topics............................................................................... 28 适配器Adapter ............................................................................................ 29 风格与主题Styles and Themes..................................................................... 29 资源和资产Resources and Assets ...................................................................... 29 资源引用Resource Reference ...................................................................... 45 国际化和本地化Internationalization and Localization ................................. 45 意图和意图过滤器Intents and Intent Filters......................................................... 45 意图过滤器Intent filters ........................................................................... 50 通常情况Common cases........................................................................... 53 使用意图匹配Using intent matching ..................................................... 54 数据存储Data Storage .......................................................................................... 54 概览Storage quickview ................................................................................ 54  系统偏好:快速,轻量级存储 .................................................................... 55  文件:存储到设备内部或可移动闪存 ......................................................... 55  数据库:任意的结构化存储 ....................................................................... 55  支持基于网络的存储 .................................................................................. 55 系统偏好Preferences .................................................................................... 55 文件Files ....................................................................................................... 56 数据库Databases .......................................................................................... 57 网络Network ................................................................................................. 57 内容提供器Content Providers.............................................................................. 57 内容提供器的基础知识Content Provider Basics .......................................... 58 查询一个内容提供器Querying a Content Provider....................................... 60 修改数据Modifying Data ............................................................................... 64 创建一个内容提供器Creating a Content Provider ........................................ 67 Content URI 总结 .......................................................................................... 70 清单文件The AndroidManifest.xml File............................................................... 71 清单文件结构Structure of the Manifest File................................................. 72 文件约定File Conventions............................................................................ 74 文件特性File Features .................................................................................. 76

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值