Styles and Themes

Style

style是一个View或者Window的外观或者格式的属性集合,它可以指定诸如高,字体颜色或者大小,背景色,padding等等属性。

Theme是用于整个application或者activity的,而不是一个view。当一个style被当作Theme用于一个application或者activity时,在application或者activity中的所有view讲使用它支持的属性。例如把下文定义的CodeFont用于一个activity,那么该activity中的所有text将会有下划线(android:typeface=”monospace”)。

style和web设计里面把stylesheets类似—-把设计和内容分开。例如定义一个TextView属性:

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

可以改成如下的方式

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

这个style相关的属性值从layout中分离,在res/values/styles.xml文件中
(文件名字任意,通常为styles.xml.但是后缀必须是.xml,且这个xml文件根部局必须是< resorces>).

创建一个style时,通过name来指定style名称,以此来区分不同的style.每添加一个属性则添加一个< item>标签。< item>的值可以是一个十六进制颜色代码,指向其他资源的引用,或者是其他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>

resource里面的每一个子元素都会在编译期间被转换为应用资源对象,可以通过< style>的name属性值引用.例如上面的TextView引用 @style/CodeFont.

继承

可以通过style的parent属性继承一个特定的style。这是可选的。继承后可以添加或者修改一个属性。继承的style可以是自定义的,也可以是系统定义的。

例如继承一个系统的style

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

当继承自定义的style时,不必使用parent属性,只需要在自定义名字后面加一个后缀名。例如继承之前的CodeFont

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

CodeFont的textColor将被覆盖。产生新的style—-CodeFont.Red。可以继承很多次。注意这种继承方式只能应用于继承自定义的style。如果继承系统style,必须用parent属性。

style属性

可以通过一个特定view的class文件xml介绍,找到哪些属性可以应用与这个view。例如一个TextView的xml属性值可以应用于一个TextView。

例如EditView的android:inputType属性:

<EditText
    android:inputType="number"
    ... />

所以你可以建立一个style

<style name="Numbers">
  <item name="android:inputType">number</item>
  ...
</style>

EditText便可以引用这个Style:

<EditText
    style="@style/Numbers"
... />

android.R.attr列举了可用于定义style的属性值,但是view并不是能接受所有的style属性值,如果一个style被用于view,而其中含有view所没有的属性,那么该view会忽略这些。

一些style只能用于Theme,而不能用于一个view。以window开头的,例如windowNoTitle、 windowBackground。只能当作一个Theme用于application或者activity。

应用style和theme于UI

1.在一个view中添加style

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

2.在< activity>或者< application>中使用android:theme.

使用自定义

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

使用系统定义的style

<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>

定义好之后就可以在activity中引用替代android:Theme.Light

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

注意,android:windowBackground只支持引用一个color,而不像android:colorBackground能指定一个颜色值

如果一个style被应用于一个ViewGroup,那么他们子view不会继承,但如果想让一个style应用于所有view,那么将它当作theme用于activity或者application,如果一个view只支持部分属性,那么他会忽略其他不支持的属性。

版本适应

当想使用新版本的theme,而又想兼容低版本时,可以基于版本,自定义相同名字Theme,而继承不同的系统Theme,将其防雨不同文件夹。

例如在res/values/styles.xml中

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

若想在Api11及其以上版本使用android:Theme.Holo.Light:可以在res/values-v11中定义

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

使用系统的Styles和Themes

android.R.style类中定义了所有可用的style。在xml中使用这些属性时,可以将其中的下划线改为’.’。例如使用Theme_NoTiTleBar. >>>>>>>>>>>>>> “@android:style/Theme.NoTitleBar“.

如果想定义一些style或者theme。(e.g., “windowBackground” or “textAppearance”),参考类R.attr或者相关的view类

R.style类中只是粗略介绍各个style。

详细介绍:

Android Themes(Themes.xml)

Android Styles (styles.xml)

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值