原文自:http://android.eoe.cn/topic/ui
category:Android Dev Guide
style是用于指定View或window的外观和格式的一系列属性的集合。style可以指定高(height)、填补(padding)、字体颜色、字体大小、背景颜色等等属性。style定义在不同于用来设置布局的XML资源中。
Android中的Syles与网页设计中的层叠样式表有着相似的原理——允许你将设计从内容中分离出来。
例如,使用一个style,你可以将下面这个布局:
变成这样:
所有与style相关的属性从XML布局中移出,放到一个名为CodeFont 的style定义中,通过style属性应用。你将在以下章节中看到此类style的定义。
theme是一个应用于整个Activity或应用中,而不是某一个单独的View(正如上面的例子)。当一个style被作为theme来应用时,Activity或应用中的每个View都将应用支持的每个style属性。例如,你能把CodeFont style作为theme应用于一个Activity,那么这个Activity中所有文本都将是绿色等宽字体。
定义样式 - Defining Styles
创建一套style,需保存一个XML文件到你的工程的res/values/ 文件夹中。
这个XML文件的根节点必须是 。
为每个要创建的style,添加一个用来唯一标识此style的 元素到文件中(这个属性是必需的)。然后为style的每个属性添加一个name 和一个使用的值(这个属性是必需的)。这个 的值可以是一个关键字符串、十六进制颜色、到另一个资源类型的引用或其他值,取决于style的属性。这里有一个单独style的例子:
<?xml version"utf-8"?>
fill_parent
wrap_content
#00FF00
monospace
每个 元素的@style/CodeFont 在一个XML布局中引用(正如上面的介绍)。
在parent 属性是可选的,用来指定另一个style资源的ID,前者继承后者的所有属性。你可以复写继承的style属性,如果你想要那样做。
记住,你想要用作一个Activity或应用theme的style,与在XML中定义一个View的style方法是一样的。一个如同上面那样定义的style可以应用于一个View的style,或是整个Activity或应用的theme。稍后讨论如何将一个style应用于一个View或一个应用theme中。
继承 - Inheritance
parent 属性让你能够从指定的style中继承属性。你可以通过这种途径从一个现有的style中继承属性,然后定义你想改变或添加的属性。你可以从你自己创建的style或平台内创建的style中继承。(参阅Using Platform Styles and Themes,以获取关于继承Android平台预定义的style的信息。)例如,你可以继承Android平台默认文本外观并修改:
#00FF00
如果你想要继承你自己定义的style,你不必使用CodeFont 的style,把颜色改为红色,你可以像这样编写新的style:
#FF0000
注意在parent 属性,因为CodeFont 起始(你已经创建的style),这个style继承所有style属性。这个style复写@style/CodeFont.Red 引用这个新style。
你可以像这样继续继承很多次,只要修改句点之前的名称。例如,你可以扩展CodeFont.Red 使字体变大:
30sp
从CodeFont.Red style中同时继承,然后添加android:textSize 属性。
* 注意:* 这种技巧仅适用于将你自己定义的资源链接起来。你不能用这种方式继承Android内建的style。要引用一个诸如TextAppearance的内建style,你必须使用parent 属性。
样式属性 - Style Properties
到目前,你已明白了一个style是如何定义的,你需要学习由 元素定义的哪些属性是可用的。你很可能已经熟悉了某些,比如layout_width和textColor。当然,有更多的style属性供你使用。
找到适用于某个特定View的属性的最佳方法是相应的类的参考,其中列出了所有支持的XML属性。例如,在表格TextView XML attributes中列出的所有属性可以用在TextView元素(或它的一个子类)的style定义中。其中列出的一个属性是android:inputType,那么你通常可能将android:inputType属性放置在 元素中,像这样:
你也可以为包含这个属性的EditText元素创建一个style:
number
...
所以你的布局XML现在可以这样实现这个style:
这个简单的例子看起来增加了工作量,但当你添加越来越多的style属性并考虑到此style在不同地方的可重用性时,你会发现获益是巨大的。
关于所有可用的style属性,请参见R.attr。记住所有的View对象并不接受相同的style属性,所以你通常应该参考特定的View类,查看其所支持的style属性。但是,如果你对一个View应用了style,而其并不支持此style中某些属性,那么此View将应用那些它支持的属性,并简单忽略那些不支持的。
然而一些style属性只能被当作一个theme来应用,而不支持任何View元素。这些style属性应用到整个窗口,而不是任何类型的View。例如那些用于隐藏应用标题、隐藏状态栏或改变窗口背景的style属性。这些style属性不属于任何View对象。探究这些仅应用作theme的style属性,参见R.attr中那些以windowNoTitle 和windowBackground 是仅当style作为theme应用于一个Activity或应用时才有效的style属性。参阅下一节,获得关于style应用作theme的信息。
* 注意:* 不要忘记对每个android: 命名空间前缀。例如: 。
样式和主题应用到UI - Applying Styles and Themes to the UI
有两种方式来设置style:
* 对一个独立的View,添加style 属性到你的布局XML中的View元素中。
* 或者,对一个Activity或应用添加 或 元素中。
当你应用一个style到布局中一个单独的View上,由此style定义的属性会仅应用于那个View。如果一个style应用到一个ViewGroup上,那么子View元素并不会继承应用此style属性——只有你直接应用了style的元素才会应用其属性。然而,你可以通过将其作为theme来应用的方式应用一个style到所有View元素上。
将一个style作为一个theme来应用,你必须在Android manifest中将其应用到一个Activity或应用中。当你这样做,此Activity或应用中的每个View都将应用其所支持的属性。例如,如果你应用前面示例中的CodeFont style到一个Activity,那么支持此文本style属性的所有View元素都将应用它们。所有View所不支持的属性都会被忽略。如果一个View仅支持某些属性,那么它就只应用那些属性。
为视图套用样式 - Apply a style to a View
下面是如何在XML布局中为View设置style的方法:
现在这个TextView将应用名为CodeFont 的style所定义的属性。(参阅前面在Defining Styles中的示例)。
* 注意:* style属性不能使用android: 命名空间前缀。
在Activity或应用程序中应用主题 - Apply a theme to an Activity or application
对你的应用程序中所有activity设置一个theme,打开 标签,使之包含android:theme 属性和style名称。例如:
如果你希望theme仅应用到你的应用程序中的某个Activity中,那么就将 标签里。
正如Android提供的其他内建资源一样,有许多你可以使用的预定义theme,而不用自己编写它们。例如,你可以使用Dialog theme使你的Activity看起来像一个对话框:
或者你想让背景变成透明的,那就使用透明theme:
如果你喜欢一个theme,但又想调整它,那么你可以将其作为你的自定义theme的parent 。例如,你可以像这样修改传统的light theme来使用你自己定义的颜色:
#b0b0ff
@color/custom_theme_color
@color/custom_theme_color
(注意,这里颜色需要作为单独的资源提供,因为android:colorBackground ,它不能得到一种文本颜色。)
然后在Android Manifest中使用Theme.Light :
选择基于平台版本的主题 - Select a theme based on platform version
新版本的Android应用程序提供额外的theme,你可能想使用它们在这些平台上运行,同时与旧版本兼容。你可以通过使用自定义theme资源选择不同的parent theme,根据平台版本之间切换完成。
例如,这里声明一个自定义theme,相当于是标准平台上默认的light theme。它将在XML文件的res/values/styles.xml ):
...
当程序运行在Android3.0(API等级11)或更高的版本时使用新的holographic theme,你可以在res/values-v11 的XML文件中放置另一个声明theme,但holographic theme的parent theme像这样设置:
...
现在可以如其他的theme那样使用这个theme,如果你的应用程序运行在Android3.0或更高的版本时,将自动切换到holographic theme。
你可以在R.styleable.Theme中找到你能够使用的theme的标准属性列表。
获得更多关于如theme和layout提供替代资源,基础平台版本或其他设备配置的详细信息,请参阅Providing Resources文档。
使用平台的样式和主题 - Using Platform Styles and Themes
Android平台提供了大量的style和theme供你在应用程序中使用。你可以在R.style类中找到所有可用的style。要使用这些style,用句点替换style名称中的下划线。例如,你可以通过"@android:style/Theme.NoTitleBar"应用Theme_NoTitleBar theme。
然而,R.style没有好的文档,没有详细叙述这些style,所以查看这些style和theme的实际源代码将使你更好理解每个style属性提供了什么功能。为更好参考Android的style和theme,请参阅下列源代码:
* Android Styles (styles.xml)
* Android Themes (themes.xml)
这些文件将通过例子帮助你学习。举个例子,在Android theme源代码中,你将会找到一个声明。在这个定义中,你将看到所有由Android框架使用的用于对话框的style属性。
为获得更多关于在XML中创建style的语法,参阅Available Resource Types:Style and Themes。
关于你可以用来定义style或theme的可用style属性(例如,"windowBackground" 或 "textAppearance"),参阅R.attr或者对应于你正在为其创建一个style的View类。
←返回User Interface | 用户界面 - User Interface
相关推荐
the defining decade!
When Android first arrived in 2008, it was almost seen as a poor relation to the much more stylish iOS on Apple iPhone. But, quite quickly, through diverse handset offers that struck a chord with both...
The Defining Decade_ Why Your T - Jay, Meg.pdf
需求分析和解决方案结构定义 Analyzing Requirements and Defining Solution Architectures
5 Defining Your Application Using the Android Manifest File 115 Configuring Android Applications Using the Android Manifest File 115 Editing the Android Manifest File 116 Managing Your Application...
Defining and Using Styles JARs and Library Projects Tutorial #6 - Adding a Library The Action Bar Tutorial #7 - Setting Up the Action Bar Android’s Process Model Activities and Their Lifecycles ...
A GraphQL service is created by defining types and fields on those types, then providing functions for each field on each type. Learn more at : http://graphql.org/learn/ OkGraphQl is an Android client...
Chapter 1: The Android Security Model – the Big Picture 7 Installing with care 7 Android platform architecture 9 Linux kernel 9 Middleware 11 Dalvik virtual machine 11 Application layer 11 Android ...
决定性的十年
Apache Beam is a unified model for defining both batch and streaming data-parallel processing pipelines, as well as a set of language-specific SDKs for constructing pipelines and Runners for executing...
Defining and Using Styles Dependencies Tutorial #6 - Adding a Library Introducing ConstraintLayout RecyclerView The Action Bar Vector Drawables Tutorial #7 - Setting Up the Action Bar Android’s ...
Android-Permission-Usage-Without-Defining 使用未定义权限的 Android 签名级权限在这个项目中有两个应用程序。 第一个,名为“first”,使用权限读取联系人并创建后台服务以在被询问时提供联系人列表。 该服务受...
Android Material App Rating This library allows to use customized Rating Dialog inside applications. Download compile 'com.stepstone.apprating:app-rating:2.3.0' Supported features custom styles for...
MAXIM公司的介绍模数转换器(ADC)各项动态参数定义,以及动态性能测试方法 第一部分 Part1
Defining your layout Working on your activity public class MainActivity extends Activity { private QuickReaderView quickReaderView; @Override protected void onCreate(Bundle savedInstanceState) { ...
MCSE+MCSD微软原版电子书MCSD Analyzing Requirements and Defining Solution.chm
AD 电路仿真分析
Flux Architecture on Android Finding a good architecture for Android applications is not easy. Google seems to not care much about it, so there is no official recommendation on patterns beyond ...
Defining Load-Balancing Policy in Static Cluster
Android lacks proper support for custom typefaces. Most obvious method of defining typeface for UI elements via XML attributes is missing from default framework views. This library makes it a lot ...