本文出自 “” 博客,请务必保留此出处

一、基本概念

作用和网页开发中的CSS是一样的。样式用在单个控件上,主题应用在整个应用或一个或多个Activity上。

二、实例代码

在res/values文件夹下建立style.xml文件,该文件中体现了样式的继承。样式的覆盖和CSS一样,也是就近原则。

 
  1. <?xmlversion="1.0"encoding="utf-8"?>

  2. <resources>

  3. <!-- 样式中设置的属性针对某个控件 -->

  4. <stylename="xyStyle">

  5. <itemname="android:textSize">18dp</item>

  6. <itemname="android:textColor">#FF0000</item>

  7. </style>

  8. <!-- 继承方式1 -->

  9. <stylename="txtViewStyle"parent="xyStyle">

  10. <itemname="android:layout_width">fill_parent</item>

  11. <itemname="android:layout_height">wrap_content</item>

  12. </style>

  13. <!-- 继承方式2 -->

  14. <stylename="txtViewStyle.child">

  15. <itemname="android:textColor">#0D9DF0</item>

  16. </style>

  17. <!-- 主题中设置的属性针对整个应用或某个Activity-->

  18. <stylename="xyTheme">

  19. <itemname="android:windowNoTitle">true</item>

  20. <!-- 表示引用android:windowNoTitle的值 -->

  21. <itemname="android:windowFullscreen">?android:windowNoTitle</item>

  22. </style>

  23. </resources>

 
  1. <?xmlversion="1.0"encoding="utf-8"?>

  2. <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"

  3. android:orientation="vertical"

  4. android:layout_width="fill_parent"

  5. android:layout_height="fill_parent">

  6. <TextViewandroid:text="@string/hello"style="@style/txtViewStyle.child"/>

  7. </LinearLayout>

 
  1. <applicationandroid:icon="@drawable/icon"android:label="@string/app_name"android:theme="@style/xyTheme">

  2. <activityandroid:name=".MainActivity"

  3. android:label="@string/app_name">

  4. <intent-filter>

  5. <actionandroid:name="android.intent.action.MAIN"/>

  6. <categoryandroid:name="android.intent.category.LAUNCHER"/>

  7. </intent-filter>

  8. </activity>

  9. </application>

  10. <uses-sdkandroid:minSdkVersion="8"/>

本文出自 “” 博客,请务必保留此出处