码迷,mamicode.com
首页 > 移动开发 > 详细

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

时间:2016-07-06 15:01:42      阅读:253      评论:0      收藏:0      [点我收藏+]

标签:

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可获取该主题的所有属性集合。

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

标签:

原文地址:http://blog.csdn.net/vanpersie_9987/article/details/51837594

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!