美文网首页
Styles and Themes 样式与主题

Styles and Themes 样式与主题

作者: chauI | 来源:发表于2016-11-14 14:56 被阅读40次

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

  • style和theme的基本用法
  • style继承和多继承
  • 不同版本设置不同theme

样式 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 的文档

相关文章

网友评论

      本文标题:Styles and Themes 样式与主题

      本文链接:https://www.haomeiwen.com/subject/phohyttx.html