android 中的样式(Style)和主题(Theme)资源

Android中支持样式文件。创建方法 “文件”→Android XML File 如下图

 

其实说白了就是在res文件下的values中创建样式文件(格式为xml)

文件是这样的:

<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:android="http://schemas.android.com/apk/res/android">
    <style name="test1">
        <item name="android:textSize" >16sp</item>
        <item name="android:textColor">#f66</item>
    </style>

 <style name="test2" parent="@style/test1">
        <item name="android:textSize" >16sp</item>
        <item name="android:textColor">#ff6</item>
    </style>
</resources>

这里的跟元素“resources” 。所有的样式都要在它里面编辑

<style></style>这就是样式的标签。name就是样式名称 我这里分别是“test1”和“test2”,红色的属性parent 就是“父样式”。注意:子样式会替换父样式。我这里父样式的字体颜色是

“#f66”,子样式会把他替换成#ff6。

<item></item> 就是指定具体的样式属性了。例如我这里的<item name="android:textColor">#f66</item>。<item>标签中的name 对应的是控件的样式。#f66就是改样式的值

<style>标签中的红色

使用方式:直接给组件指定style属性即可,语法格式“@[package_name:]style.file_name”。style=@style/样式名称

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:orientation="vertical"
 android:layout_width="fill_parent"
 android:layout_height="fill_parent"
 >
<EditText
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
 style="@style/test1"
    />
</LinearLayout>

如果有html基础的一看就明白了。。。

 

主题资源:主题资源可以指定这个应用程序的样式或者具体的Activity样式

指定具体的Activity样式:可以在onCreate方法中使用setTheme(android.R.style.test1);也可以在AndroidManiFest.xml文件中 给指定的Activity标签 添加 android:theme="@style/test1"

整个应用程序:在AndroidManiFest.xml文件中 给application 标签 添加 android:theme="@style/test1"

 

 

posted @ 2013-09-25 21:59  仰望 星空  阅读(710)  评论(0编辑  收藏  举报