Android中的样式与CSS样式作用相似,都是用于为界面元素定义显示风格,它是一个包含一个或者多个view控件属性的集合。
例如,在CSS中定义字体的颜色和大小:
<style type="text/css">
.mycss{color:#FFFFCC; font-size:18px;}
</style>
使用上面的CSS样式:<div class="mycss">使用样式</div>
Android中的样式的使用:
我们先来看看没有使用样式的情况:
res/layout文件夹下activity_main.xml文件
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<TextView
android:id="@+id/testview1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="没有使用样式的TestView" />
</RelativeLayout>
没有使用样式的界面的效果:
在styles.xml文件里声明样式的情况:
res/values文件夹下的styles.xml
<resources>
<!--
Base application theme, dependent on API level. This theme is replaced
by AppBaseTheme from res/values-vXX/styles.xml on newer devices.
-->
<style name="AppBaseTheme" parent="android:Theme.Light">
<!--
Theme customizations available in newer API levels can go in
res/values-vXX/styles.xml, while customizations related to
backward-compatibility can go here.
-->
</style>
<!-- Application theme. -->
<style name="AppTheme" parent="AppBaseTheme">
<!-- All customizations that are NOT specific to a particular API-level can go here. -->
</style>
<!--mystyle1-->
<style name="textViewMyStyle1">
<item name="android:textSize">32sp</item>
<item name="android:textColor">#FF0000</item>
</style>
</resources>
在布局文件中应用自己定义的样式:
res/layout文件夹下activity_main.xml文件
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<TextView
android:id="@+id/testview1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="@style/textViewMyStyle1"
android:text="使用样式的TestView" />
</RelativeLayout>
使用样式的布局界面效果:
样式也是可以继承的
3.1第一种继承方式:
res/values文件夹下的styles.xml
<resources xmlns:android="http://schemas.android.com/apk/res/android">
<!--
Base application theme, dependent on API level. This theme is replaced
by AppBaseTheme from res/values-vXX/styles.xml on newer devices.
-->
<style name="AppBaseTheme" parent="android:Theme.Light">
<!--
Theme customizations available in newer API levels can go in
res/values-vXX/styles.xml, while customizations related to
backward-compatibility can go here.
-->
</style>
<!-- Application theme. -->
<style name="AppTheme" parent="AppBaseTheme">
<!-- All customizations that are NOT specific to a particular API-level can go here. -->
</style>
<!--mystyle1-->
<style name="textViewStyle1">
<item name="android:textSize">32sp</item>
<item name="android:textColor">#FF0000</item>
</style>
<!--mystyle2继承mystyle1的第一中方法 -->
<!--mystyle2-->
<style name="textViewStyle2" parent="textViewStyle1">
<item name="android:background">#0000cc</item>
<!-- 覆盖父mystyle1的字体颜色属性 -->
<item name="android:textColor">#00FF00</item>
</style>
</resources>
在布局文件中应用自己定义的样式:
res/layout文件夹下activity_main.xml文件
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<TextView
android:id="@+id/testview1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="@style/textViewStyle2"
android:text="继承样式的TestView1" />
</RelativeLayout>
使用样式的布局界面效果:
3.2第二种继承方式:
res/values文件夹下的styles.xml
<resources xmlns:android="http://schemas.android.com/apk/res/android">
<!--
Base application theme, dependent on API level. This theme is replaced
by AppBaseTheme from res/values-vXX/styles.xml on newer devices.
-->
<style name="AppBaseTheme" parent="android:Theme.Light">
<!--
Theme customizations available in newer API levels can go in
res/values-vXX/styles.xml, while customizations related to
backward-compatibility can go here.
-->
</style>
<!-- Application theme. -->
<style name="AppTheme" parent="AppBaseTheme">
<!-- All customizations that are NOT specific to a particular API-level can go here. -->
</style>
<!--mystyle1-->
<style name="textViewStyle1">
<item name="android:textSize">33sp</item>
<item name="android:textColor">#FF0000</item>
</style>
<!--mystyle2继承mystyle1的第二中方法 -->
<!--mystyle2-->
<style name="textViewStyle1.textViewStyle2" >
<item name="android:background">#0000cc</item>
<!-- 覆盖父mystyle1的字体颜色属性 -->
<item name="android:textColor">#00FF00</item>
</style>
</resources>
在布局文件中应用自己定义的样式:
res/layout文件夹下activity_main.xml文件
Android中的主题是用于为应用定义显示风格,它的定义和样式的定义相同。
res/values文件夹下的styles.xml
<resources xmlns:android="http://schemas.android.com/apk/res/android">
<!--
Base application theme, dependent on API level. This theme is replaced
by AppBaseTheme from res/values-vXX/styles.xml on newer devices.
-->
<style name="AppBaseTheme" parent="android:Theme.Light">
<!--
Theme customizations available in newer API levels can go in
res/values-vXX/styles.xml, while customizations related to
backward-compatibility can go here.
-->
</style>
<!-- Application theme. -->
<style name="AppTheme" parent="AppBaseTheme">
<!-- All customizations that are NOT specific to a particular API-level can go here. -->
</style>
<!--mystyle1-->
<style name="textViewStyle1">
<item name="android:textSize">33sp</item>
<item name="android:textColor">#FF0000</item>
</style>
<!--mystyle2继承mystyle1的第二中方法 -->
<!--mystyle2-->
<style name="textViewStyle1.textViewStyle2" >
<item name="android:background">#0000cc</item>
<!-- 覆盖父mystyle1的字体颜色属性 -->
<item name="android:textColor">#00FF00</item>
</style>
<!-- 定义全屏主题 -->
<style name="myTheme">
<!-- 没标题 -->
<item name="android:windowNoTitle">true</item>
<!-- 全屏显示 -->
<item name="android:windowFullscreen">?android:windowNoTitle</item>
</style>
</resources>
上面"?android:windowNoTitle"中的问号用于引用在当前主题中已定义的资源的值。
将应用与整个应用程序
在AndroidManifest.xml中为应用设置上面定义的主题:
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/myTheme" >
效果:
除了在AndroidManifest.xml中可以设置主题,也可以在代码中设置主题,如下:
setTheme(R.style.myTheme);
尽管在定义上,样式和主题基本相同,但是它们使用的地方不同:
1.样式用于单独的View,例如:EditText、TextView等;
2.主题通过AndroidManifest.xml中的<application>和<activity>用于整个应用或者某个Activity,主题对整个应用或某个Activity进行全局性影响。
3.如果一个应用使用了主题,同时应用下的View也使用了样式,那么当主题和样式属性发生冲突时,样式的优先级高于主题。
此外系统定义的一些常有主题:
对话框风格
<activity android:theme="@android:style/Theme.Dialog"></activity>
透明风格
<activity android:theme="@android:style/Theme.Translucent"></activity>