Android API Guide-styles and Themes

Source: Internet
Author: User
<span id="Label3"></p><p><p>a <span style="line-height: 1.5;">style is a collection of properties that specify the appearance and format of a view or window. The style can specify only Height,padding,font Color,font size,background color, and many other Properties. The style is defined in the XML resource file</span> and is <span style="line-height: 1.5;">independent of the layout XML file. </span>the style in Android has a similar design philosophy to CSS in the web-allowing separation of design styles and Content.</p></p><p><p>For example, using a style, you can put such a layout XML:</p></p><pre><pre><span style="color: #0000ff;"><</span> <span style="color: #800000;">TextView </span> <span style="color: #ff0000;">Android:layout_width</span> <span style="color: #0000ff;">= "fill_parent"</span> <span style="color: #ff0000;"> android:layout_height</span><span style="color: #0000ff;">= "wrap_content"</span><span style="color: #ff0000;"> android:textcolor</span><span style="color: #0000ff;">= "#00FF00" </span> <span style="color: #ff0000;"> android:typeface</span><span style="color: #0000ff;">= "monospace"</span><span style="color: #ff0000;"> android:text</span><span style="color: #0000ff;">= "@string/ Hello "</span><span style="color: #0000ff;">/></span></pre></pre><p><p>Convert the following code:</p></p><pre><pre><span style="color: #0000ff;"><</span> <span style="color: #800000;">TextView </span> <span style="color: #ff0000;">style</span> <span style="color: #0000ff;">= "@style/codefont"</span> <span style="color: #ff0000;"> android:text</span><span style="color: #0000ff;">= "@string/hello"</span><span style="color: #0000ff;">/></span></pre></pre><p><p>All style-related properties are removed from the layout XML and placed in a style called codefont, which is referenced by the Control's style Property. You can see this style in the next Section.</p></p><p><p>Theme is a style that is referenced by the entire activity or app rather than by a single view (like The example above). When a style is used as a theme, all the properties specified by the style are referenced by the activity or by the properties supported by each view in the app. For example, you can apply a style like Codefont as a theme to an activity, and then all the text in the activity becomes a green monospace Font.</p></p>Define Style<p><p>You just need to save the XML file in the Project's res/values/directory, and you can create a style. The name of the XML file is arbitrary, but the file extension must be. XML and must be saved under the Res/values/folder.</p></p><p><p>Adding the <style> element to the file, the name attribute in all the style is the unique identity of the style (this property is required). Then add a <item> element for each Style's specified property, the name of the element to declare the style property you want to define, and the value of the property (the value is required). The value of item can be a string with a special meaning, a hexadecimal color value, a reference to another resource type, or a value related to the style Property. Here is an example of a single style file:</p></p><pre><span style="color: #0000ff;"><span style="color: #0000ff;"><?</span></span><span style="color: #ff00ff;"><span style="color: #ff00ff;">XML version= "1.0" encoding= "utf-8"</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">?></span></span><span style="color: #0000ff;"><span style="color: #0000ff;"><</span></span><span style="color: #800000;"><span style="color: #800000;">Resources</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">></span></span> <span style="color: #0000ff;"><span style="color: #0000ff;"><</span></span><span style="color: #800000;"><span style="color: #800000;">style</span></span><span style="color: #ff0000;"><span style="color: #ff0000;">name</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">= "codefont"</span></span><span style="color: #ff0000;"><span style="color: #ff0000;">Parent</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">= "@android: style/textappearance.medium"</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">></span></span> <span style="color: #0000ff;"><span style="color: #0000ff;"><</span></span><span style="color: #800000;"><span style="color: #800000;">Item</span></span><span style="color: #ff0000;"><span style="color: #ff0000;">name</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">= "android:layout_width"</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">></span></span>Fill_parent<span style="color: #0000ff;"><span style="color: #0000ff;"></</span></span><span style="color: #800000;"><span style="color: #800000;">Item</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">></span></span> <span style="color: #0000ff;"><span style="color: #0000ff;"><</span></span><span style="color: #800000;"><span style="color: #800000;">Item</span></span><span style="color: #ff0000;"><span style="color: #ff0000;">name</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">= "android:layout_height"</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">></span></span>Wrap_content<span style="color: #0000ff;"><span style="color: #0000ff;"></</span></span><span style="color: #800000;"><span style="color: #800000;">Item</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">></span></span> <span style="color: #0000ff;"><span style="color: #0000ff;"><</span></span><span style="color: #800000;"><span style="color: #800000;">Item</span></span><span style="color: #ff0000;"><span style="color: #ff0000;">name</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">= "android:textcolor"</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">></span></span>#00FF00<span style="color: #0000ff;"><span style="color: #0000ff;"></</span></span><span style="color: #800000;"><span style="color: #800000;">Item</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">></span></span> <span style="color: #0000ff;"><span style="color: #0000ff;"><</span></span><span style="color: #800000;"><span style="color: #800000;">Item</span></span><span style="color: #ff0000;"><span style="color: #ff0000;">name</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">= "android:typeface"</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">></span></span>Monospace<span style="color: #0000ff;"><span style="color: #0000ff;"></</span></span><span style="color: #800000;"><span style="color: #800000;">Item</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">></span></span> <span style="color: #0000ff;"><span style="color: #0000ff;"></</span></span><span style="color: #800000;"><span style="color: #800000;">style</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">></span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></</span></span><span style="color: #800000;"><span style="color: #800000;">Resources</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">></span></span></pre><p><p>All child elements of,<resources> are converted to an App's resource object at compile time, and this object can be referenced using the name value defined in the <style> element. The style in the example can be used in an XML layout file<code>@style/CodeFont来引用(示例在上面的介绍中)。</code></p></p><p><p>The Parent property of the <style> element is optional and is used to specify styles that inherit from other resource ids. You can also override inherited styles in <style> if you want.</p></p><p><p>Note that the style used in the activity or app and the style defined for the view are defined exactly the same in the XML file. For example, the style in the example above can define the style of a single view or be used as a theme in activity and in the app. We'll discuss later how to use style to define a style for a single style or as a theme for an app.</p></p><p><p>(translated and compiled from Https://developer.android.com/intl/zh-cn/guide/topics/ui/themes.html#DefiningStyles)</p></p><p><p>Android API Guide-styles and Themes</p></p></span>

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.