今天来和大家分享一下android中UI设计里面常会用到的style和theme。

首先,style和theme都是资源,android提供了很多这样的默认资源。你可以来使用它们。同时你也可以自己定义style和theme。这非常的简单,只需要在res/values/这个路径里面新建一个.xml文件,而且他的根节点必须是<resources>.对每一个style和theme,给<style>element增加一个全局唯一的名字,也可以选择增加一个父类属性,我们写的style和theme就会继承这个父类的属性。style和theme的定义格式相同。不过style是针对view来说的,比如TextView,EditText这些,而theme必须针对整个activity或者整个程序,你必须在AndroidManifest.xml中的<application>或者<activity>中定义。 

先来看看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>

相关文章: