<span id="Label3"></p><p><p><span style="font-size: 18px">Today we talk about the introduction of our own definition control in android, in android, we generally write XML is a single control to complete, But. Often in some projects. A single control is sometimes not enough. So we can define the control ourselves, the advantage of defining the control with our own is that it is more flexible on one hand, and the other is that it is more efficient to define the control in the case of large data volume than the write layout file. Other places to use this control just need to reference the control to be Able.</span></p></p><p><p></p></p><p><p><span style="font-size: 18px">Here's How to write a custom control and where it needs to be noted:</span></p></p><p><p><span style="font-size: 18px"><strong> <span style="color: #ff0000">1</span></strong>. Create an XML file to write the layout you want to define yourself</span></p></p><p><p><span style="font-size: 18px"><strong><span style="color: #ff0000">2</span></strong>. Create a <span style="color: #33cc00">attrs.xml</span> <span style="color: #99ff99"> </span> structure in <span style="color: #009900">res/values</span> as follows</span></p></p><p><p><span style="font-size: 18px"></span></p></p><pre name="code" class="java"><pre name="code" class="java"> <resources> <declare-styleable name= "myshow" > <attr name= "tvtitle" format= "string"/> <attr name= "titleminlength" format= "integer"/> <attr name= "tvhint" format= "string"/> < attr name= "title" format= "color"/> <attr name= "tvvalue" format= "string"/> </declare-styleable ></resources></pre></pre><br><p><p></p></p><p><p><span style="font-size: 18px"><br></span></p></p><p><p><span style="font-size: 18px">Describes the type of the <span style="font-size: 18px">format property and the multiple specified types</span> in each attr tag</span></p></p><p><p><span style="font-size: 18px">1 "reference"//reference to a resource ID</span></p></p><p><p><span style="font-size: 18px"></span></p></p><pre name="code" class="java"><pre name="code" class="java"><declare-styleable name = "name" > <attr name = "background" format = "reference"/> </pre></pre><br><p><p></p></p><p><p><span style="font-size: 18px">Use:<br></span></p></p><pre name="code" class="java"><pre name="code" class="java"> <imageview android:layout_width = "42dp" android:layout_height = "42dp" android:background = "@ Drawable/image ID " /></pre></pre><br><p><p></p></p><p><p><span style="font-size: 18px"><br></span></p></p><p><p><span style="font-size: 18px">"2"<span style="font-size: 18px">"color"//color</span></span></p></p><p><p><span style="font-size: 18px"><span style="font-size: 18px"><span style="font-size: 18px"> </span><span style="font-size: 18px"></span></span></span></p></p><pre name="code" class="java"><pre name="code" class="java"> <declare-styleable name = "name" > <attr name = "textcolor" format = "color"/> </declare-styleable& Gt</pre></pre><br><span style="font-size: 18px"><span style="font-size: 18px"></span></span><p><p></p></p><p><p><span style="font-size: 18px">Use:</span></p></p><p><p><span style="font-size: 18px"></span></p></p><span style="font-size: 18px"><span style="font-size: 18px"></span></span><pre name="code" class="java"><pre name="code" class="java"><textview android:layout_width = "42dp" android:layout_height = "42dp" Android:textcolor = "#ffffff" /></pre></pre><br><span style="font-size: 18px"><span style="font-size: 18px"></span></span><p><p><span style="font-size: 18px"></span></p></p><p><p><span style="font-size: 18px"><span style="font-size: 18px"><br></span></span></p></p><p><p><span style="font-size: 18px"><span style="font-size: 18px"><span style="font-size: 18px">"3" <span style="font-size: 18px">"boolean"//boolean Value</span></span></span></span></p></p><p><p><span style="font-size: 18px"> </span><span style="font-size: 18px"> <span style="font-size: 18px"> </span></span></p></p><pre name="code" class="java"><pre name="code" class="java"><declare-styleable name = "name" > <attr name = "focusable" format = "boolean"/> </ Declare-styleable></pre></pre><br><span style="font-size: 18px"><span style="font-size: 18px"></span></span><p><p></p></p><p><p><span style="font-size: 18px">Use:</span></p></p><p><p><span style="font-size: 18px"><span style="font-size: 18px"></span></span></p></p><pre name="code" class="java"><pre name="code" class="java"><button android:layout_width = "42dp" android:layout_height = "42dp" android:focusable = "true" / ></pre></pre><br><span style="font-size: 18px"><span style="font-size: 18px"></span></span><br><p><p></p></p><p><p><span style="font-size: 18px">"4"<span style="font-size: 18px">"dimension"//dimension Value</span><br></span><span style="font-size: 18px"></span><span style="font-size: 18px"></span></p></p><pre name="code" class="java"><pre name="code" class="java"> <declare-styleable name = "name" > <attr name = "layout_width" format = "dimension"/> </ Declare-styleable></pre></pre><br><span style="font-size: 18px"><span style="font-size: 18px"></span></span><p><p></p></p><p><p><span style="font-size: 18px">Use:</span></p></p><p><p><span style="font-size: 18px"><span style="font-size: 18px"></span></span></p></p><pre name="code" class="java"><pre name="code" class="java"> <button android:layout_width = "42dp" android:layout_height = "42dp" /></pre></pre><br><span style="font-size: 18px"><span style="font-size: 18px"></span></span><p><p></p></p><p><p><span style="font-size: 18px">5<span style="font-size: 18px">"float"//floating-point Value</span></span></p></p><p><p><span style="font-size: 18px"><span style="font-size: 18px"> </span></span></p></p><pre name="code" class="java"><pre name="code" class="java"><declare-styleable name = "alphaanimation" > <attr name = "fromalpha" format = "float"/> <attr na me = "toalpha" format = "float"/> </declare-styleable></pre></pre><br><p><p></p></p><p><p><span style="font-size: 18px">Use:</span></p></p><p><p><span style="font-size: 18px"></span></p></p><pre name="code" class="java"><pre name="code" class="java"> <alpha android:fromalpha = "1.0" Android:toalpha = "0.5" /></pre></pre><br><p><p></p></p><p><p><span style="font-size: 18px">"6"<span style="font-size: 18px">"integer"//integer Value</span></span></p></p><p><p><span style="font-size: 18px"><span style="font-size: 18px"></span></span></p></p><pre name="code" class="java"><pre name="code" class="java"><declare-styleable name = "animatedrotatedrawable" > <attr name = "visible"/> <attr name = " Frameduration "format=" integer "/> <attr name =" framescount "format=" integer "/> <attr name =" Pivotx "/> <attr name =" pivoty "/> <attr name =" drawable "/> </declare-styleable></pre></pre><br><p><p></p></p><p><p><span style="font-size: 18px">Use:</span></p></p><p><p><span style="font-size: 18px"></span></p></p><pre name="code" class="java"><pre name="code" class="java"> <animated-rotate xmlns:android = "http://schemas.android.com/apk/res/android" android:drawable = "@ Drawable/image ID " android:pivotx =" 50% " android:pivoty =" 50% " android:framescount =" android " : Frameduration = "/>" </pre></pre><br><p><p></p></p><p><p><span style="font-size: 18px">"7"<span style="font-size: 18px">"string"//string</span></span></p></p><p><p><span style="font-size: 18px"><span style="font-size: 18px"> </span><span style="font-size: 18px"> </span><span style="font-size: 18px"> </span></span></p></p><pre name="code" class="java"><pre name="code" class="java"><declare-styleable name = "alphaanimation" > <attr name = "strName format =" string "/> <attr name = "strtitle" format = "string"/> </declare-styleable></pre></pre><br><span style="font-size: 18px"><span style="font-size: 18px"></span></span><p><p></p></p><p><p><span style="font-size: 18px">Use:</span></p></p><p><p><span style="font-size: 18px"></span></p></p><pre name="code" class="java"><pre name="code" class="java"><com.google.android.maps.mapview android:layout_width = "fill_parent" android:layout_height = "fill_ Parent " Android:apikey =" 0jokq80od1jl9c6haja99ugxcris2cgjko " /></pre></pre><br><p><p></p></p><p><p><span style="font-size: 18px">"8"<span style="font-size: 18px">"fraction"//percentage, For example 200%</span></span></p></p><p><p><span style="font-size: 18px"></span></p></p><pre name="code" class="java"><pre name="code" class="java"><declare-styleable name= "rotatedrawable" > <attr name = "visible"/> <attr name = "fromdegrees" format = "float"/> <attr name = "todegrees" format = "float"/> <attr name = "pivotx" format = "fract Ion "/> <attr name =" pivoty "format =" fraction "/> <attr name =" drawable "/> </declare-st Yleable></pre></pre><br><p><p></p></p><p><p><span style="font-size: 18px">Use:</span></p></p><p><p><span style="font-size: 18px"></span></p></p><pre name="code" class="java"><pre name="code" class="java"><rotate xmlns:android = "http://schemas.android.com/apk/res/android" android:interpolator = "@anim/animation ID " android:fromdegrees =" 0 " android:todegrees =" android:pivotx " =" 200% " android:pivoty =" 300% " C6/>android:duration = "android:repeatmode" = "restart" android:repeatcount = "infinite" /></pre></pre><br><p><p></p></p><p><p><span style="font-size: 18px">"enum"//enumeration Value of "9"</span></p></p><p><p><span style="font-size: 18px"></span></p></p><pre name="code" class="java"><pre name="code" class="java"><declare-styleable name= "name" > <attr name= "orientation" > <enum name= "horizontal" value= "0"/ > <enum name= "vertical" value= "1"/> </attr> </declare-styleable></pre></pre><br><br><p><p></p></p><p><p><span style="font-size: 18px">Use:</span></p></p><p><p><span style="font-size: 18px"></span></p></p><pre name="code" class="java"><pre name="code" class="java"><linearlayout xmlns:android = "http://schemas.android.com/apk/res/android" android:orientation = " Vertical\horizontal " android:layout_width =" fill_parent " android:layout_height =" fill_parent " > </LinearLayout></pre></pre><br><p><p><span style="font-size: 18px"></span></p></p><p><p><span style="font-size: 18px"><br></span></p></p><p><p><span style="font-size: 18px">"10"</span></p></p><p><p><span style="font-size: 18px">Note: you can specify more than one type value when a property is defined</span></p></p><p><p><span style="font-size: 18px"></span></p></p><pre name="code" class="java"><pre name="code" class="java"><declare-styleable name = "name" > <attr name = "background" format = "reference|color"/> </ Declare-styleable></pre></pre><br><br>Use:<br><pre name="code" class="java"><pre name="code" class="java"> <imageview android:layout_width = "42dip" android:layout_height = "42dip" android:background = "@ Drawable/picture id| #ffffff " /></pre></pre><br><br><p><p></p></p><p><p><span style="font-size: 18px"><span style="color: #ff0000">3</span>. Define a class that you define to inherit a view inherit which view depends on your actual situation.</span></p></p><p><p><span style="font-size: 18px"></span></p></p><p><p><span style="font-size: 18px"></span></p></p><p><p><span style="font-size: 18px"></span></p></p><p><p><span style="font-size: 18px"><span style="color: #ff0000">4</span>. Refer to your own defined class to the XML you need</span></p></p><p><p><span style="font-size: 18px"><1> must refer to his current location path before adding such as:</span></p></p><p><p><span style="font-size: 18px">xmlns:fx= "http://schemas.android.com/apk/res/<span style="color: #33cc00">fx.com.bitmapsolution</span>"<br></span></p></p><p><p><span style="font-size: 18px"><2> referencing detailed controls and getting a property in the control</span></p></p><p><p><span style="font-size: 18px"><fx.com.bitmapsolution.myview<br>Android:layout_width= "fill_parent"<br>android:layout_height= "wrap_content"<br><span style="background-color: rgb(51,204,0)">fx:tvtitle= "@string/title3"</span><br><span style="background-color: rgb(51,204,0)">fx:titleminlength= "4"</span><br>android:layout_margintop= "5dp"<br>/></span></p></p><p><p><span style="font-size: 18px">and that's it. a simpler own definition control complex point own definition control there is time to write another hahaha ~ ~</span></p></p><p><p><span style="font-size: 18px"></span></p></p><p><p><span style="font-size: 18px"></span></p></p><p><p><span style="font-size: 18px"></span></p></p><p><p><span style="font-size: 18px"><br></span><br></p></p><p><p><br><br><br><br></p></p><p><p><span style="font-size: 18px"><br></span></p></p><p><p><span style="font-size: 18px"></span></p></p><p><p></p></p> <p><p>Android self-defined controls</p></p></span>
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