(整理得比较草,用于以后查找)
res/values/
directory of your project. The name of the XML file is arbitrary, but it must use the
.xml
extension and be saved in the
res/values/
folder.
The best place to find properties that apply to a specific View
is the corresponding class reference, which lists all of the supported XML attributes. For example, all of the attributes listed in the table of TextView XML attributescan be used in a style definition for a TextView
element (or one of its subclasses). One of the attributes listed in the reference is android:inputType
, so where you might normally place the android:inputType
attribute in an<EditText>
element, like this:
<EditText android:inputType="number" ... />
R.style
class.
int | Theme_Holo_Dialog_NoActionBar_MinWidth | Variant of Theme.Holo.Dialog.NoActionBar that has a nice minimum width for a regular dialog. |
R.style
for Android styles and themes
The R.style
reference, however, is not well documented and does not thoroughly describe the styles, so viewing the actual source code for these styles and themes will give you a better understanding of what style properties each one provides. For a better reference to the Android styles and themes, see the following source code:
- Android Styles (styles.xml) -----也即这里: https://android.googlesource.com/platform/frameworks/base/+/refs/heads/master/core/res/res/values/styles.xml
- Android Themes (themes.xml) -----也即这里: https://android.googlesource.com/platform/frameworks/base/+/refs/heads/master/core/res/res/values/themes.xml
These files will help you learn through example. For instance, in the Android themes source code, you'll find a declaration for <style name="Theme.Dialog">
. In this definition, you'll see all of the properties that are used to style dialogs that are used by the Android framework.
Theme.Holo
theme (or one of its descendants), which is the default theme when either the
targetSdkVersion
or
minSdkVersion
attribute is set to
"11"
or greater. For example:
<?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 name="CodeFont.Red"> <item name="android:textColor">#FF0000</item> </style>
<TextView style="@style/CodeFont" android:text="@string/hello" />
<application android:theme="@style/CustomTheme">
<activity android:theme="@android:style/Theme.Dialog">
For example, here is the declaration for a custom theme which is simply the standard platforms default light theme. It would go in an XML file under res/values
(typically res/values/styles.xml
):
<style name="LightThemeSelector" parent="android:Theme.Light"> ... </style>
To have this theme use the newer holographic theme when the application is running on Android 3.0 (API Level 11) or higher, you can place an alternative declaration for the theme in an XML file in res/values-v11
, but make the parent theme the holographic theme:
<style name="LightThemeSelector" parent="android:Theme.Holo.Light"> ... </style>
Now use this theme like you would any other, and your application will automatically switch to the holographic theme if running on Android 3.0 or higher.