Styles and Themes 样式与主题

2016-11-14  本文已影响40人  chauI

参考 Android开发入门:样式和主题(Styles and Themes)

样式 styles

res/values下新建的XML,根节点为<resources>。
每一个<item>元素里的name来指定样式的属性,以及和样式属性对应的值。

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

<!-- 另一个文件中 -->
<?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>中的parent指定继承的属性,
也可以通过<style>中的name属性,比如继承上面的style,它的name为CodeFont,
那么新建的style

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

就可以继承CodeFont的属性,引用时为CodeFont.input,这种方法可以多次继承。

主题 Themes

主题可以让一个activity或者全部(一个程序里)activity继承多个style
Androidmanifest.xml中,分别为程序和单独的activity设置:
<application android:theme="@style/CustomTheme">
<activity android:theme="@android:style/Theme.Dialog">

在不同的版本设置不同的主题

可以在根目录下新建 res/values-v11,对应Android 3.0(API Level 11)

Paste_Image.png

另:theme 的文档

上一篇 下一篇

猜你喜欢

热点阅读