Android guide and android programming authoritative guide

Source: Internet
Author: User

Android guide and android programming authoritative guide
Styles and Themes)

A Style is a set of properties that apply to a View control or Window. A style can specify many attributes, such as height, fill, font color, font size, and background color. A Style is defined in an xml resource file and distinguished from the layout specified in xml.

 

Style in Android shares a similar principle with CSS in Web Design. They allow you to separate Design and Content ).

 

For example, by using a style, you can make your layout xml as follows:

 

<TextView    android:layout_width="fill_parent"    android:layout_height="wrap_content"    android:textColor="#00FF00"    android:typeface="monospace"    android:text="@string/hello" />

In addition, it can be changed to the following:

<TextView>
<?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>

The child node of each <resources> element is converted into a program resource object during compilation. It can be referenced through the value of the name attribute of the <style> element. This sample style can be referenced in the XML layout, for example, "@ style/CodeFont" (as shown in the preceding example ).

 

The parent attribute in the <style> element is optional and points to the resource ID of another style. It can inherit some attributes from this style. If appropriate, You can override the attributes inherited by override.

 

Remember, a style you want to use in the activity or application is defined in the XML file, and all view controls have the same style. A style, such as the one defined above, can be used as a single view control for a style application or as a theme applied to all the activities or applications. We will discuss how to apply a single view or as the topic of an application later.

 

Inheritance

The parent attribute in <style> allows you to specify a style that you can inherit from its attributes. You can use this to inherit attributes from an existing style and define some unique attributes that you need to change or add. You can inherit the Styles and Themes that you have created or that you have built on the Platform, for more information about inheriting the Android platform style ). For example, you can inherit the default font style of Android and modify it, for example:

  <style name="GreenText" parent="@android:style/TextAppearance">        <item name="android:textColor">#00FF00</item>    </style>

 

If you want to inherit the style you created, you do not need to apply the parent attribute. As an alternative, you can build a prefix for the name of the style you want to inherit and use it as the name of your new style, separated by "points" in the middle. For example, to create a style, such as inheriting from the above CodeFont style, but changing the color to red, you can write your new style as follows:

<style name="CodeFont.Red">        <item name="android:textColor">#FF0000</item>    </style>

Note that the <style> label does not have the parent attribute. However, because the name attribute starts with the style name CodeFont (which is a defined style, the style inherits all the style attributes from. This style overwrites the android: textColor attribute and changes the text to red. You can reference this new style, such as @ style/CodeFont. Red.

 

You can continue to inherit these names for many times by using the "dot" link. For example, you can expand CodeFont. Red, as shown below:

  <style name="CodeFont.Red.Big">        <item name="android:textSize">30sp</item>    </style>

Here CodeFont and CodeFont. Red are inherited, And the android: textSize attribute is added.

 

Note: This method can be inherited by link names and can only be used for styles in your own defined resource files. You cannot inherit the built-in style of Android. To reference a built-in style, for exampleTextAppearance, You must use the parent attribute.

 

Style attributes

Now that you understand how to define a style, you need to learn what types of style attributes are defined under the <item> element. You may already be familiar with many existing ones, suchlayout_widthAndtextColor. Of course, there are many style attributes you can use.

 

The best place to find the attributes that can be applied to a specified view control is the corresponding class reference, which lists all supported XML attributes. For example, all attributes listed in TextView XML attributes can be applied to TextView elements (or its subclass) in a style definition ). A property listed in the reference is calledAndroid: inputType, so that you can apply this attribute to the <EditText> element, as shown below:

<EditText    android:inputType="number"    ... />

You can create a style for the EditText element, which includes this attribute to replace the above:

<Style name = "Numbers">
<Item name = "android: inputType"> number </item>
...
</Style>

In this way, the XML used for your layout can implement the style as follows:
<EditText   ><TextView   ><application android:theme="@style/CustomTheme">

If you want to use a style for an activity in your application, addandroid:themeTo your <activity> tag.

Android provides some built-in resources. You can use these predefined styles instead of writing them again. For example, you can use the Dialog topic to make your Activity look like a Dialog box:

<activity android:theme="@android:style/Theme.Dialog">

Or you want to make your background transparent and use transparent themes:

<activity android:theme="@android:style/Theme.Translucent">

If you like a topic but want to use it with full horsepower, you can add it to the parent attribute in your custom topic to inherit it. For example, you can modify the traditional light theme and use your own color, as shown below:

<color name="custom_theme_color">#b0b0ff</color><style name="CustomTheme" parent="android:Theme.Light">    <item name="android:windowBackground">@color/custom_theme_color</item>    <item name="android:colorBackground">@color/custom_theme_color</item></style>

Note that the colors used above are provided in other resources. Here, because the android: windowBackground attribute only supports referencing other resources, unlikeAndroid: colorBackground attribute specifies a color description text.

Now, in your Mnaifest file, you can useCustomThemeReplace Theme. Light:

<activity android:theme="@style/CustomTheme">

 

Select the style supported by a platform as the basis

The newer versions of Android provide additional available styles for applications, and you may want to use them to run on those platforms and remain compatible with older versions. You can use custom styles to switch between different parent themes based on the platform version.

For example, the following is a custom style Declaration, which is a simple standard platform default light theme topic. It may be stored in the XML file in the res/valeus folder (classic: res/values/styles. xml ).

<style name="LightThemeSelector" parent="android:Theme.Light">    ...</style>

When an application runs on Android 3.0 (API level 11) or later, the holo style topic can be used for this style, you can place an optional declarative style to prevent XML files in the res/values-v11 folder, But let this style inherit from a holo style topic:

<style name="LightThemeSelector" parent="android:Theme.Holo.Light">    ...</style>

 

Now you can use this topic like any other one, and when running on Android 3.0 or later, your application will automatically switch to the holo style topic.

InR.styleable.Theme., You can find a standard attribute list for your use.

For more information, such as themes and la S, switching resource file selection based on the platform version or device configuration, you can refer to Providing Resources.

 

Use platform styles and themes

The Android platform provides many styles and themes for your use in applications. InR.styleClass. To use the styles listed here, you can use the "dot" to replace all the underlined style names. For example, you can use""@android:style/Theme.NoTitleBar". To useTheme_NoTitleBarStyle.

 

 R.styleReferences. However, it is not a good file record and cannot fully describe the style, then, you can view the actual resource code of those styles and themes to better understand the "various provided style attributes. Obtain

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.