Android-styles and Themes (theme)

Source: Internet
Author: User
<span id="Label3"></p><p><p>If we often need to specify a roughly similar format for a component of a certain type, such as font, color, background color, and so on, if you repeatedly set these properties on the view component each time, this certainly increases the workload and is not conducive to late maintenance of the Project.</p></p> <p><p>In fact, the style of Android is a bit like css, if you have experience in the development of Web site students, to understand style, is a better understanding.</p></p> <p><p><span style="background-color: rgb(255, 0, 0);"><strong><span style="font-size: 20px; font-family: ‘arial black‘, ‘avant garde‘; background-color: rgb(255, 0, 0);">Style resources</span></strong></span></p></p> <p><p>The Android style resource file is also placed in the/res/values directory, the root element of the style resource file is the <resource.../> element, and in <resource. /> can also include multiple <style.../> sub-elements, each <style. the/> element defines a style. The style element defines the following properties:<br></p></p> <p><p></p></p> <pre class="brush:xml;toolbar: true; auto-links: false;"><pre class="brush:xml;toolbar: true; auto-links: false;">name: specifies the names of the styles parent: specifies the parent style to which the style inherits, and when a parent style is inherited, the style will have all the formatting defined in the parent Style. of course, The current style can also override the formatting in the parent Style.</pre></pre> <pre class="brush:xml;toolbar: true; auto-links: false;"><pre class="brush:xml;toolbar: true; auto-links: false;"><style.../> elements can contain multiple <item.../> child elements, each <item.../> child element defines a format item</pre></pre> <p><p>Examples are as follows<span style="line-height: 24.0000019073486px; font-size: 12.5px;"> </span> :</p></p> <pre class="brush:xml;toolbar: true; auto-links: false;"><pre class="brush:xml;toolbar:true; auto-links:false;"><?xml version= "1.0"  encodig= "utf-8"?> <resource>    <style name= "style1" >         <item name= "android:textsize" >20sp</item>         <item name= "android:textcolor" > #00d </item>    </style>     <style name= "style2"  parent= "style1" >         <item name= "android:background" > #ee6 </item>         <item name= "android:padding" >8dp</item>         <!--override the property specified in the parent style-->        <item name= " Android:textcolor "> #000 </item>    </style></resource> </pre></pre> <p><p>The above style resource defines two styles, where the second style inherits the first style, and the TextColor property in the second style overrides the TextColor property in the parent Style.</p></p> <p><p>Once you have defined the above style resource, you can then use the style in the XML resource in the following syntax format:</p></p> <pre class="brush:xml;toolbar: true; auto-links: false;"><pre class="brush:xml;toolbar: true; auto-links: false;">@[<package_name>]style/file_name</pre></pre> <p><p>The following is the interface layout file in this instance, which contains two text boxes, each of which uses two Styles.</p></p> <pre class="brush:xml;toolbar: true; auto-links: false;"><linearlayout xmlns:android= "http://schemas.android.com/apk/res/android"      Android:layout_width= "match_parent"     android:layout_height= "match_parent"      android:orientation= "vertical" >    <EditText         android:layout_width= "match_parent"          android:layout_height= "wrap_content"         style= "@style/style1"         android:text= "@string/style1"/>    < Edittext        android:layout_width= "match_parent"          android:layout_height= "wrap_content"          style= "@style/style2"         android:text= "@string/style2"/> </LinearLayout></pre> <p><p>The above interface layout file does not specify EN and format for the two text boxes, except that they specify a style that uses style1.style2, and the formatting that these two styles contain is applied to both text Boxes.</p></p> <p><p><span style="font-family: ‘arial black‘, ‘avant garde‘; font-size: 20px; background-color: rgb(255, 0, 0);">Topic Resources</span></p></p> <p><p>Similar to style resources, subject resource XML files are often also placed in the Res/values directory, and the XML document for the subject resource also uses the <resource.../> element as the root element, as well as the <style.../> element to define the Subject.</p></p> <p><p>The differences between themes and styles are reflected in:</p></p> <ul> <ul> <li><p>A theme cannot act on a single view component, and the subject should work for all the activity of the entire application, or to the specified activity</p></li> <li><p>The format of the theme definition should be a format that changes the appearance of the window, such as the window Caption. Window borders, etc.</p></li> </ul> </ul> <p><p> <span style="font-size:16px; font-family:impact, chicago;"> <strong> instance: Add borders, backgrounds     to all windows                                                                                                                                        </strong> </span> </p></p> <p><p>The usage of the topic is described below with an example. To add a border to all windows, a background, and a theme in The/res/values/my_style.xml file, the <style.../> fragment of the definition body is as follows:<br></p></p> <pre class="brush:xml;toolbar: true; auto-links: false;"><pre class="brush:xml;toolbar: true; auto-links: false;"><?xml version= "1.0" encoding= "utf-8"?> <resources> <style name= "crazy_theme" > <item name= "a Ndroid:windownotitle ">true</item> <item name=" android:windowfullscreen ">true</item> < ; Item Name= "android:windowframe" > @drawable/abc_btn_check_material</item> <item name= "android: Windowbackground "> @drawable/abc_btn_check_to_on_mtrl_015</item> </style></resources></pre></pre> <p><p>Once you have defined the above topics, you can then use this topic in Java code, such as</p></p> <pre class="brush:java;toolbar: true; auto-links: false;"><pre class="brush:java;toolbar: true; auto-links: false;">public class Mainactivity extends Activity{private listview listview;        @Override public void OnCreate (Bundle Savedinstancestate) {super.oncreate (savedinstancestate);        SetTheme (r.style.crazy_theme);    Setcontentview (r.layout.activity_main); }}</pre></pre> <p><p>Most of the time, it's easier to apply a theme to a specific app or activity in a androidmanifest.xml file. If we want all forms of the app to use this theme, just add the Android:theme property to the <application.../> element, the property value is the name of a topic, as shown in the following code:</p></p> <pre class="brush:xml;toolbar: true; auto-links: false;"><pre class="brush:xml;toolbar: true; auto-links: false;"><application android:theme= "@style/crazyittheme" >...</application></pre></pre> <p><p>If you point to an activity in your program that has this theme, you can modify it to the <activity.../> element, and the same android:theme specifies the Subject.</p></p> <p><p>This application is <span style="font-size: 13.3333339691162px; line-height: 24.0000019073486px;"><application in the Androidmanifest.xml file. The Android:theme= "@style/crazytheme" attribute is added to the/> element. </span></p></p> <p><p><span style="font-size: 13.3333339691162px; line-height: 24.0000019073486px;">Several built-in theme resources are available in android, which can be seen by querying the Android.r.style class.</span></p></p> <p><p>Similar to styling, Android themes also support Inheritance. If you want to take advantage of a topic during development, but need to modify it locally, you can implement a custom theme by inheriting the system Theme. For example, the following code snippet:</p></p> <pre class="brush:xml;toolbar: true; auto-links: false;"><pre class="brush:xml;toolbar: true; auto-links: false;"><style name= "crazytheme" parent= "@android: style/theme.dialog" >...</style></pre></pre> <p><p>The Crazytheme theme defined above inherits the Android.r.style.theme.dialog theme, so the next step in the <style.../> element is to add the <item.../> Child elements can override some of the properties of a system Theme.</p></p><p><p>Android-styles and Themes (theme)</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.