The meaning of Defstyle in the third parameter of the custom style in Android and the View's constructor

Source: Internet
Author: User
<span id="Label3"></p><span id="OSC_h1_1"><span id="OSC_h1_1"></span></span>The meaning of Defstyle in the third parameter of the custom style in Android and the View's constructor<span id="OSC_h1_2"><span id="OSC_h1_2"></span></span>0. Preface<p><p>System-brought view can be configured in XML properties, for good custom view can also be configured in XML properties, in order to make the custom view properties can be configured in xml, the following 4 steps are Required:</p></p> <ol> <ol> <li><p><br></p></li> <ol> <li><p>Add a property to a custom view by <declare-styleable></p></li> <li><p>Declaring property values in XML for the corresponding property</p></li> <li><p>Get property values at run time (typically Constructors)</p></li> <li><p>Applies the Acquired property value to the view</p></li> </ol> </ol> </ol><p><p>How to apply the obtained attribute value to the view it is needless to say that the properties that you have defined are definitely clear, so look at the first three Points.</p></p><span id="OSC_h1_3"><span id="OSC_h1_3"></span></span>one, Custom Style<p><p>Declare the attributes required by the custom view through the <declare-styleable> element, here is an example, the file is Res/values/attrs.xml</p></p><pre><pre><?xml version= "1.0" encoding= "utf-8"? ><resources><declare-styleable name= "Customize" ><attr Name= "attr_one" format= "string"/><attr name= "attr_two" format= "string"/><attr name= "attr_three" format= "string"/><attr name= "attr_four" format= "string"/></declare-styleable> <attr name= " Customizestyle "format=" Reference "/></resources></pre></pre><p><p>In the XML above, we declare that customize and customizesyle,customize contain four attr_three of attr_one, attr_two, attr_four, and attribute, Customizestyle is also a attribute, but it is not declared in Declare-styleable.</p></p><p><p>Definitions in declare-styleable are not materially different from those defined directly with attr, in the XML above, whether Attr_one-attr_four is declared in declare-styleable, The system will generate 5 attribute for us in r.attr</p></p><pre><pre>public static final class attr {public static final int customizestyle=0x7f010004;public static final int attr_one=0x7f010 000;public static final int attr_two=0x7f010001;public static final int attr_three=0x7f010002;public static final int attr _four=0x7f010003;}</pre></pre><p><p>The difference is that if declared in declare-styleable, the system will also generate related properties for us in R.styleable.</p></p><pre><pre>public static final class styleable {public static final int[] Customize = {0x7f010000, 0x7f010001, 0x7f010002, 0x7f010003    };p ublic static final int customize_attr_one = 0;public static final int customize_attr_two = 1; public static final int customize_attr_three = 2;public static final int customize_attr_four = 3;}</pre></pre><p><p>As shown above, r.styleable.customize is a int[], and the value of the element inside is exactly the same as R.attr.attr_one-r.attr.attr_four one by one, and r.styleable.customize_attr 4 values, such as _one, are the indexes of R.attr.attr_one-r.attr.attr_four in the R.styleable.customize array. This array and index will be used when the property value is obtained at the third step of the run, and the function of attr group declaration in Declare-styleabe is that the system will automatically generate these things for us, if not declared in declare-styleable, We can build this array ourselves when we need it, because the array is built by itself, and the subscript index of each attribute is very clear, just a bit cumbersome. The above opinion, but from the use and experimental difficult to see that is True.</p></p><span id="OSC_h1_4"><span id="OSC_h1_4"></span></span>Declaring property values for corresponding attributes in XML<p><p>We know there are several different ways to assign a property to an attribute in XML</p></p> <ol> <ol> <li><p>Using attributes directly in layout</p></li> <li><p>Set style and set properties in style</p></li> <li><p>Application and activity can specify theme, and you can specify the default values for the properties in the current application or activity in theme</p></li> </ol> </ol><p><p>Here are the three ways to look at each of these</p></p><span id="OSC_h3_5"><span id="OSC_h3_5"></span></span>1. Using attributes directly in layout<p><p>Using custom attributes in XML layout is about the same as using System properties, but the properties belong to a different namespace, such as the Following.</p></p><pre><pre><relativelayout xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:ad= "http// Schemas.android.com/apk/res/com.angeldevil.customstyle "xmlns:tools=" Http://schemas.android.com/tools "android: Layout_width= "match_parent" android:layout_height= "match_parent" tools:context= ". Mainactivity "><com.angeldevil.customstyle.customtextviewandroid:layout_width=" wrap_content "android: layout_height= "wrap_content" ad:attr_one= "attr one in xml" style= "@style/throughstyle" android:text= "@string/hello_ World "/></relativelayout></pre></pre><p><p>Properties such as Layout_width belong to Android namespace, the custom properties belong to the current program namespace, just like the namespace of the declaration of android, the current program namespace is good, Just replace the red part of Android with the package name of the current Program. You also need to be aware of the namespace of attributes when applying attributes.</p></p><span id="OSC_h3_6"><span id="OSC_h3_6"></span></span>2. Set style and set properties in style<p><p>Looking at the Green Line in the XML above, we declared a style:throughstyle for customtextview, which is very simple, just specifying the value of two properties</p></p><pre><pre><style name= "throughstyle" ><item name= "attr_one" >attr one from Style</item><item name= "attr_two ">attr-style</item></style></pre></pre><p><p>Note that in the style we declare the value of the attr_one, and also in the XML directly to the Attr_one value, and finally with which one has a priority problem, followed by the third section: at run time to get the value of the property in addition, next look at the third way.</p></p><span id="OSC_h3_7"><span id="OSC_h3_7"></span></span>3. Specify the default value of the property in the current application or activity in theme<pre><pre> <!-- application theme. --><style name= "apptheme"  parent= "AppBaseTheme" ><!-- All customizations that are NOT specific to a  Particular api-level can go here. --><item name= "attr_one" >attr  One from theme</item><item name= "attr_two" >attr two from theme</ Item><item name= "attr_three" >attr three from theme</item><item name = "customizestyle" > @style/customizestyleintheme</item></style><style name= " Customizestyleintheme "><item name=" Attr_one ">attr one from theme reference </item><item name= "attr_two" >attr two from theme reference</item> <item name= "attr_three" >attr three from theme reference</item></style </pre></pre><p><p>In the XML above, we specified values for attr_one, attr_two, and Attr_three in apptheme, but also a customizestyle for reference, and reference in this ATTR_ one, attr_two, and Attr_three specify values, and Customizestyle is a property that we define in Attrs.xml. There are two ways to set a value for a property in theme, either by defining it directly in theme or by referencing a style through another attribute, which can be seen in the following section getting property Values.</p></p><span id="OSC_h1_8"><span id="OSC_h1_8"></span></span>third, get the property value at run time<p><p>In styles.xml, We also define a Defaultcustomizestyle style, which can be seen under the Action.</p></p><pre><pre><style name= "defaultcustomizestyle" ><item name= "attr_one" >attr one from Defalut style res</item> <item name= "attr_two" >attr from Defalut style res</item><item name= "attr_three" >attr three from Defalut style res</item></style></pre></pre><p><p>First look at the Customtextview Code.</p></p><pre>public class customtextview extends textview {private static final  String tag = customtextview.class.getsimplename ();p Ublic customtextview (Context  Context)  {super (context);     }public customtextview (context context,  Attributeset attrs)  {this (context, attrs, r.attr.customizestyle);     } Public customtextview (context context, attributeset attrs, int defstyle)  { Super (context, attrs, defstyle);         typedarray a  = context.obtainstyledattributes (attrs, r.styleable.customize, defstyle,  r.style.defaultcustomizestyle);        string one =  A.getstring (r.styleable.customize_attr_one);         string two  = a.getstring (r.styleable.customize_attr_two);         string three = a.getstring ( r.styleable.customize_attr_three);         string four =  a.getstring (r.styleable.customize_attr_four);         log.i (TAG,   "one:"  + one);         log.i (tag,  "one:"  +  two);         log.i (tag,  "three:"  + three);  &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;LOG.I (tag,  "four:"  + four);         a.recycle ();     }}</pre><p><p>The main code is in the third function, there is nothing to do here, just through the context of the obtainstyledattributes obtained the previously defined value of 4 properties and printed Out.</p></p><span id="OSC_h3_9"><span id="OSC_h3_9"></span></span>The third parameter of the third constructor of view Defstyle<p><p>If a view is instantiated in code, the first constructor is called, and if the definition in XML calls the second constructor, and the third function system is not called, by view (our custom or system-predefined view, So Customtextview and Button) are explicitly called, for example here we call the third constructor in the second constructor and pass the R.attr.customizestyle to the third Argument.</p></p><p><p>The meaning of the third parameter, as its name implies, is the default style, but it is not clear here that the default style here refers to the default style in the current application or activity theme, with the button in the system as an Example.</p></p><pre><pre>Public Button (context Context) {this (context, null);} Public Button (context context, attributeset attrs) {this (context, attrs, com.android.internal.r.attr.buttonstyle);} Public Button (context context, attributeset attrs, int defstyle) {super (context, attrs, defstyle);}</pre></pre><p><p>Instantiating a view in code invokes the first constructor, which in XML calls the second constructor, which calls the third constructor in the implementation of the Button. And the value of Defstyle is Com.android.internal.r.attr.buttonstyle. ButtonStyle is a attribute defined in the system, the system has a theme by default, such as 4.0 is Theme.holo</p></p><pre><pre><style name= "theme.devicedefault" parent= "theme.holo" >...<item name= "buttonstyle" > @android: style/ Widget.devicedefault.button</item>....</style></pre></pre><p><p>Above is the system default theme, which specifies a style for ButtonStyle</p></p><pre><pre><style name= "Widget.DeviceDefault.Button" parent= "Widget.Holo.Button" ></style><style name= " Widget.Holo.Button "parent=" Widget.button "><item name=" android:background "> @android:d rawable/btn_ Default_holo_dark</item><item name= "android:textappearance" >?android:attr/textappearancemedium</ Item><item name= "android:textcolor" > @android: color/primary_text_holo_dark</item><item name= " Android:minheight ">48dip</item><item name=" android:minwidth ">64dip</item></style></pre></pre><p><p>This style defines the default properties of the button in the system, such as Background.</p></p><p><p>As you can see from the description of the third constructor in the document, the function of this constructor is that the subclass of the view provides the underlying style of the class</p></p> <blockquote> <blockquote> <p>View (context context, attributeset attrs, int Defstyleattr)</p> <p>Perform inflation from XML and <span style="color: #ff0000;">apply a class-specific base style</span>.</p> </blockquote> </blockquote><p><p>The above said are more abstract, or directly look at the instance code to clear, the experiment with the code above all pasted, here directly see the results, but before this to see a function: obtainstyledatributes.</p></p><span id="OSC_h3_10"><span id="OSC_h3_10"></span></span>Obtainstyledatributes<p><p>The property values we are going to get are obtained by the Typedarray returned by this function, which is the official note: obtainstyledattributes, the following is the function prototype:</p></p><pre><pre>Public TypedArray obtainstyledattributes (attributeset set, int[] attrs, int defstyleattr, int defstyleres)</pre></pre><p><p>The meanings of the 4 parameters are:</p></p><p><p>Set: a collection of property values</p></p><p><p>Attrs: an array of the resource IDs of the properties we want to get, like the projection array when requesting a database in contextprovider, is the value of what property we want to query from a bunch of properties</p></p><p><p>Defstyleattr: This is a attribute in the current theme, a reference to the style, when no properties are specified for the view in the layout XML and Style. Will find the corresponding property value from the style that this attribute points to in theme, this is the meaning of defstyle, if you do not specify a property value, use this value, so it is the default value, but this attribute to be specified in theme, and is a reference to a style, if this parameter passed in 0 means that the default value is not searched for theme</p></p><p><p>Defstyleres: This is also a resource ID that points to a style, but it works only if defstyleattr is 0 or defstyleattr is not 0 but theme does not assign a value to the Defstyleattr property</p></p><p><p>The link to this function description is barely passable, here is a brief summary. For a property, you can specify its value in multiple places, such as XML direct definition, style,theme, and the values defined by these locations have a priority, from high to low by priority:</p></p> <blockquote> <blockquote> <p>Define the >style definition in XML directly > The default values specified by Defstyleattr and Defstyleres > values specified directly in theme</p> </blockquote> </blockquote><p><p>Look at this relationship is more clear, defstyleattr and defstyleres in the previous parameter description has been said, "directly in the theme value specified" meaning in the following code can be Seen.</p></p><span id="OSC_h3_11"><span id="OSC_h3_11"></span></span>Experimental validation<p><p>The relevant code has been affixed to the front, but for the convenience of viewing, here the relevant XML pasted together again</p></p><pre>main_activity.xml<relativelayout xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:ad= " Http://schemas.android.com/apk/res/com.angeldevil.customstyle "xmlns:tools=" Http://schemas.android.com/tools " Android:layout_width= "match_parent" android:layout_height= "match_parent" tools:context= ". Mainactivity " ><com.angeldevil.customstyle.customtextviewandroid:layout_width=" wrap_content "android : layout_height= "wrap_content" ad:attr_one= "attr one in xml" style= "@style/throughstyle" android: text= "@string/hello_world"  /></relativelayout>attrs.xml<?xml version= "1.0"  encoding = "utf-8"? ><resources><declare-styleable name= "Customize" ><attr name= "attr_one"  format= "string"  /><attr name= "attr_two"  format= "string"  /><attr  Name= "attr_three"  format= "string"  /><attr name= "attr_four"  format= "string"  / ></declare-styleable><attr name= "customizestyle"  format= "reference"  /></resources>styles.xml<resources><style  name= "appbasetheme"  parent= "android:Theme.Light" ></style><!-- Application  Theme. --><style name= "apptheme"  parent= "appbasetheme" ><!-- All  customizations that are not specific to a particular api-level  Can go here. --><item name= "attr_one" >attr one from theme</item ><item name= "attr_two" >attr two from theme</item><item name= "attr_ Three ">attr three from theme</item><item name=" CustomizeStyle "> @style/ Customizestyleintheme</item></style><style name= "customizestyleintheme" ><item  Name= "attr_one" >attr one from theme reference</item><item name= "attr_two" >attr two from thEme reference</item><item name= "attr_three" >attr three from theme  Reference</item></style><style name= "throughstyle" ><item name= "attr_one" > Attr one from style</item><item name= "attr_two" >attr two from  Style</item></style><style name= "defaultcustomizestyle" ><item name= "attr_one" >attr one from defalut style res</item><item name= "attr_two" >attr  two from defalut style res</item><item name= "attr_three" >attr  Three from defalut style res</item></style></resources></pre><p><p><span style="line-height: 1.5;">in code, this gets the Property's</span></p></p><pre><pre>Public Customtextview (context context, attributeset attrs) {this (context, attrs, r.attr.customizestyle);}            Public Customtextview (context context, attributeset attrs, int defstyle) {super (context, attrs, defstyle); TypedArray a = context.obtainstyledattributes (attrs, r.styleable.customize, defstyle,r.style.defaultcustomizestyle)    ; ...}</pre></pre><p><p>Customizestyle is defined as a attribute,defaultcustomizestyle is defined by a style.</p></p><p><p>As you can see from the code,<span style="color: #ff0000;">R.attr.customizestyle is the Defstyleattr,r.style.defaultcustomizestyle that was mentioned earlier Defstyleres. </span></p></p><p><p>In Styles.xml we define a theme:apptheme for the app, which defines the attribute values directly for attr_one, attr_two, and Attr_three in this theme, which is what the previous relationship says <span style="color: #ff0000;"> The value specified directly in the Theme. </span></p></p><p><p>The customizestyle is defined in apptheme, a style is referenced, and the values of attr_one, attr_two, attr_three, Attr_four are printed in Customtextview's constructor, respectively. So below we can verify the previous relationship with the log output, if a attribute defines a value in multiple locations, which one works.</p></p><p><p>  </p></p><p><p>As shown in the Following:</p></p> <ul> <ul> <li><p><br></p></li> <ul> <li><p>Attr_one values are defined directly in xml, style, defstyleattr, defstyleres, and theme, but are defined in XML</p></li> <li><p>Attr_two also defines values directly in style, defstyleattr, defstyleres, and theme, but is defined in the style</p></li> <li><p>Attr_three also defines values directly in defstyleattr, defstyleres, and theme, but it works defstyleattr</p></li> <li><p>Attr_four is defined in defstyleres, but the result is still 0.</p></li> </ul> </ul> </ul><p><p>This can indicate:</p></p> <blockquote> <blockquote> <p>1. Define the >style definition in XML directly > the value defined by defstyleattr >defstyleres the specified default value, and the value specified directly in theme</p> <p>2. defstyleattr (I.E. Defstyle) is not 0 and the attribute definition can be found in the current theme, defstyleres does not work, so attr_ Four although defined in Defstyleres (defaultcustomizestyle), the value that is taken is still null.</p> </blockquote> </blockquote><p><p><span style="line-height: 1.5;">In order to look at the role of defstyleres, 1. We add the definition of Attr_four in apptheme, 2. The third argument to Obtainstyledattributes is passed in 0, or the definition of Customizestyle in Apptheme is removed, and the result is the same</span></p></p><pre><pre><style name= "apptheme" parent= "appbasetheme" ><item name= "attr_one" >attr one from theme</item>< Item name= "attr_two" >attr-theme</item><item name= "attr_three" >attr three from Theme</item ><item name= "attr_four" >attr four from theme</item></style></pre></pre><p><p>Since defstyleattr is 0, or defstyleattr is not 0 but we do not assign a value to this property, defstyleres works, when an attribute is not assigned in XML and style, The system looks for the value of the property at Defstyleres (here, defaultcustomizestyle).</p></p><p><p>  </p></p><p><p>We see that the value of Attr_three comes from defstyleres, and the value of Attr_four is derived from the direct definition in theme (defaultcustomizestyle, attr_one, atrr_two defined), That means</p></p> <blockquote> <blockquote> <p>1. Defstyleres only works when DEFSTYLEATRTR is Defstyle 0 or theme does not have a defstyle defined</p> <p>2. Defstyleres> defined directly in the theme</p> </blockquote> </blockquote><span id="OSC_h1_12"><span id="OSC_h1_12"></span></span>Conclusion<p><p>The following conclusions can be drawn from the previous instructions:</p></p> <ol> <ol> <li><p><br></p></li> <ol> <li><p>To customize a view custom property, you can declare the desired property by declare-styleable</p></li> <li><p>In order to set the default style of view in theme, you can define a property att_a with format reference, specifying a style for this attribute when defining Theme. And in the second constructor of view, the<span style="line-height: 1.5;"> third constructor is called R.attr.attr_a as the third argument</span></p></li> <li><p>You can define a style as the default value for the View property when Attr_a is not defined in Theme.</p></li> <li><p>You can assign a value directly to a property in theme, but with the lowest priority</p></li> <li><p>Defstyleres (specified by the fourth parameter of Obtainstyledattributes) does not work when Defstyleattr (that is, the third parameter of the View's Constructor) is not 0 and is assigned to this attr in theme</p></li> <li><p>Priority of property value definition: default sytle> default Style (specified by fourth parameter of obtainstyledattributes) in Xml>style>theme > Specify property values directly in theme</p></li> </ol> </ol> </ol><p><p>Code download: Customstyle.zip</p></p><p><p>The meaning of Defstyle in the third parameter of the custom style in Android and the View's constructor</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.