02_android Application Interface Programming _01_ View component

Source: Internet
Author: User
<span id="Label3"></p>1. View components and container components <blockquote> <blockquote> <p>Most of Android's UI components are placed in Android.widget and Android.view packages and their child packages, all UI components inherit the view class, and ViewGroup are used in containers for other components.</p> </blockquote> </blockquote> <table style="color: #000000"> <tbody> <tr bgcolor="#330000" valign="middle"> <td valign="top"><p align="center">XML <span style="font-family: 宋体">Properties</span></p></td> <td valign="top"><p align="center">Related methods</p></td> <td valign="top"><p align="center">Description</p></td> </tr> <tr> <td valign="top">Android:alpha</td> <td valign="top">Setalpha (float)</td> <td valign="top">Set the transparency of the component</td> </tr> <tr> <td valign="top">Android:background</td> <td valign="top">Setbackgroundresource (int)</td> <td valign="top">Sets the background color of the component</td> </tr> <tr> <td valign="top">Android:clickable</td> <td valign="top">Setclickable (boolean)</td> <td valign="top">Sets whether the component can fire a click event</td> </tr> <tr> <td valign="top">Android:contentdescription</td> <td valign="top">Setcontentdescription (charsequence)</td> <td valign="top">Set the main description information for this component</td> </tr> <tr> <td valign="top">Android:drawingcachequality</td> <td valign="top">Setdrawingcachequality (iint)</td> <td valign="top">Sets the quality of the drawing cache used by this component</td> </tr> <tr> <td valign="top">Android:fadescrollbars</td> <td valign="top">Setscrollbarfadingenabled (boolean)</td> <td valign="top">Whether to fade out the scroll bar when the Component's scroll bar is not used</td> </tr> <tr> <td valign="top">Android:fadingedge</td> <td valign="top">Setverticalfadingedgeenabled (boolean)</td> <td valign="top">Sets whether component boundaries use fade-out effects when scrolling the component</td> </tr> <tr> <td valign="top">Android:fadingedgelength</td> <td valign="top">Getverticalfadingedgelength ()</td> <td valign="top">Set the length of the fade-out boundary</td> </tr> <tr> <td valign="top">Android:focusable</td> <td valign="top">Setfocusable (boolean)</td> <td valign="top">Sets whether the component can get focus</td> </tr> <tr> <td valign="top">Android:focusableintouchmode</td> <td valign="top">Setfocusableintouchmode (boolean)</td> <td valign="top">Sets whether the component can get focus in touch mode</td> </tr> <tr> <td valign="top">Android:id</td> <td valign="top">SetId (int)</td> <td valign="top">Sets the unique identity of the Component. <span style="font-family: 宋体">in Java code, you can</span> <span style="font-family: times new roman"></span> <span style="font-family: 宋体">Get it</span> through findviewbyid.</td> </tr> <tr> <td valign="top">Android:isscrollcontainer</td> <td valign="top">Setscrollcontainer (boolean)</td> <td valign="top">Sets whether the component is used as a scrollable container</td> </tr> <tr> <td valign="top">Android:keepscreenon</td> <td valign="top">Setkeepscreenon (boolean)</td> <td valign="top">Sets whether the component will force the phone screen to remain open</td> </tr> <tr> <td valign="top">Android:longclickable</td> <td valign="top">Setlongclickable (boolean)</td> <td valign="top">Sets whether the component can respond to long click events</td> </tr> <tr> <td valign="top">Android:minheight</td> <td valign="top">Setminimumheight (int)</td> <td valign="top">Set the minimum height of the component</td> </tr> <tr> <td valign="top">Android:minwidth</td> <td valign="top">SETMINIMUMWIDTHT (int)</td> <td valign="top">Sets the minimum width of the component</td> </tr> <tr> <td valign="top">Android:nextfocusdown</td> <td valign="top">Setnextfocusdownid (int)</td> <td valign="top">Sets the component <span style="font-family: times new roman">ID</span> that gets focus when the focus is set on the component and the DOWN ARROW key is pressed</td> </tr> <tr> <td valign="top">Android:nextfocusleft</td> <td valign="top">Setnextfocusleftid (int)</td> <td valign="top">Sets the component <span style="font-family: times new roman">ID</span> that focuses on the component and gets the focus when the left arrow is pressed</td> </tr> <tr> <td valign="top">Android:nextfocusright</td> <td valign="top">Setnextfocusrightid (int)</td> <td valign="top">Sets the component <span style="font-family: times new roman">ID</span> that gets focus when the focus is on the component and the right ARROW key is pressed</td> </tr> <tr> <td valign="top">Android:nextfocusup</td> <td valign="top">Setnextfocusupid (int)</td> <td valign="top">Sets the component <span style="font-family: times new roman">ID</span> that gets focus when the focus is on the component and the UP ARROW key is pressed</td> </tr> <tr> <td valign="top">Android:onclick</td> <td valign="top"></td> <td valign="top">Binding listeners for the Component's click events</td> </tr> <tr> <td valign="top">Android:padding</td> <td valign="top">Setpadding (int,int,int,int)</td> <td valign="top">To set the fill area on four sides of a component</td> </tr> <tr> <td valign="top">Android:paddingleft</td> <td valign="top">Setpadding (int,int,int,int)</td> <td valign="top">Set the fill area on the left side of the component</td> </tr> <tr> <td valign="top">Android:paddingtop</td> <td valign="top">Setpadding (int,int,int,int)</td> <td valign="top">Set fill area on top of component</td> </tr> <tr> <td valign="top">Android:paddingright</td> <td valign="top">Setpadding (int,int,int,int)</td> <td valign="top">Set the fill area on the right side of the component</td> </tr> <tr> <td valign="top">Android:paddingbottom</td> <td valign="top">Setpadding (int,int,int,int)</td> <td valign="top">Set the fill area below the component</td> </tr> <tr> <td valign="top">Android:rotation</td> <td valign="top">Setrotation (float)</td> <td valign="top">Sets the angle at which the component rotates</td> </tr> <tr> <td valign="top">Android:rotationx</td> <td valign="top">Setrotationx (float)</td> <td valign="top">Sets the angle at which the component <span style="font-family: 宋体">rotates around the x-axis</span></td> </tr> <tr> <td valign="top">Android:rotationy</td> <td valign="top">Setrotationy (float)</td> <td valign="top">Sets the angle at which the component <span style="font-family: 宋体">rotates around the y-axis</span></td> </tr> <tr> <td valign="top">Android:saveenabled</td> <td valign="top">Setsaveenabled (boolean)</td> <td valign="top">If set to False<span style="font-family: 宋体">, the state of the component is not saved when it is frozen</span></td> </tr> <tr> <td valign="top">Android:scalex</td> <td valign="top">Setscalex (float)</td> <td valign="top">Sets the scaling ratio of the component in the horizontal direction</td> </tr> <tr> <td valign="top">Android:scaley</td> <td valign="top">Setscaley (float)</td> <td valign="top">Sets the zoom ratio of the component in the vertical direction</td> </tr> <tr> <td valign="top">Android:scrollx</td> <td valign="top"></td> <td valign="top">Horizontal scrolling offset after initialization of the component</td> </tr> <tr> <td valign="top">Android:scrolly</td> <td valign="top"></td> <td valign="top">Vertical scrolling offset after initialization of this component</td> </tr> <tr> <td valign="top">Android:scrollbaralwaysdrawhorizontaltrack</td> <td valign="top"></td> <td valign="top">Sets whether the component always displays the track of the horizontal scroll bar</td> </tr> <tr> <td valign="top">Android:scrollbaralwaysdrawverticaltrack</td> <td valign="top"></td> <td valign="top">Sets whether the component always displays the track of the vertical scroll bar</td> </tr> <tr> <td valign="top">Android:scrollbardefaultdelaybeforefade</td> <td valign="top">Setscrollbardefaultdelaybeforefade (int)</td> <td valign="top">Set how many milliseconds the scrollbar is delayed before fading out</td> </tr> <tr> <td valign="top">Android:scrollbarfadeduration</td> <td valign="top">Setscrollbarfadeduration (int)</td> <td valign="top">How many seconds does it take to set the scrollbar fade-out process</td> </tr> <tr> <td valign="top">Android:scrollbarsize</td> <td valign="top">Setscrollbarsize (int)</td> <td valign="top">Sets the width of the vertical scroll bar and the height of the horizontal scroll bar</td> </tr> <tr> <td valign="top">Android:scrollbarstyle</td> <td valign="top">Setscrollbarstyle (int)</td> <td valign="top">Sets the style and position of the scroll bar. This property supports the following properties: insideoverlay insideinset outsideoverlay outsideinset</td> </tr> <tr> <td valign="top">Android:scrollbarthumbhorizontal</td> <td valign="top"></td> <td valign="top"><span style="font-family: times new roman">drawable</span> <span style="font-family: 宋体">object</span> that sets the slider for the horizontal scroll bar of the component</td> </tr> <tr> <td valign="top">Android:scrollbarthumbvertical</td> <td valign="top"></td> <td valign="top">Sets the <span style="font-family: times new roman">drawable</span> <span style="font-family: 宋体">Object</span> for the slider of the Component's vertical scroll bar</td> </tr> <tr> <td valign="top">Android:scrollbartrackhorizontal</td> <td valign="top"></td> <td valign="top"><span style="font-family: times new roman">drawable</span> <span style="font-family: 宋体">Object</span> that sets the track for the horizontal scroll bar of the component</td> </tr> <tr> <td valign="top">Android:scrollbartrackvertical</td> <td valign="top"></td> <td valign="top">Sets the <span style="font-family: times new roman">drawable</span> <span style="font-family: 宋体">Object</span> for The track of the Component's vertical scroll bar</td> </tr> <tr> <td valign="top">Android:scrollbars</td> <td valign="top"></td> <td valign="top">Defines how several scroll bars are displayed when the component is scrolled, which supports the following property Values. None<span style="font-family: 宋体">: Scroll bar</span> horizont<span style="font-family: 宋体">: Show horizontal scroll</span> bar vertical<span style="font-family: 宋体">: Show vertical scroll bar</span></td> </tr> <tr> <td valign="top">Android:soundeffectsenabled</td> <td valign="top">Setsoundeffectsenabled (boolean)</td> <td valign="top">Sets whether the component uses sound effects when clicked</td> </tr> <tr> <td valign="top"><p align="left">Android:tag</p></td> <td valign="top"></td> <td valign="top">Sets the tag value of a string type for the component <span style="font-family: 宋体">. The string can then be obtained through the</span> <span style="font-family: times new roman">View</span> <span style="font-family: 宋体">'s</span> <span style="font-family: times new roman">gettag ()</span> <span style="font-family: 宋体">, or by</span> <span style="font-family: times new roman">Findviewwithtag ()</span> to <span style="font-family: 宋体">find the component</span></td> </tr> <tr> <td valign="top">Android:transformpivotx</td> <td valign="top">Setpivotx (float)</td> <td valign="top">Sets the <span style="font-family: times new roman">X</span> - <span style="font-family: 宋体">coordinate</span> of the center of rotation when the component rotates</td> </tr> <tr> <td valign="top">Android:transformpivoty</td> <td valign="top">Setpivoty (float)</td> <td valign="top">Sets the <span style="font-family: times new roman">Y</span> - <span style="font-family: 宋体">coordinate</span> of the center of rotation when the component rotates</td> </tr> <tr> <td valign="top">Android:translationx</td> <td valign="top">Settranslationx (float)</td> <td valign="top">Set the component to shift in the X <span style="font-family: 宋体">direction</span></td> </tr> <tr> <td valign="top">Android:translationy</td> <td valign="top">Settranslationy (float)</td> <td valign="top">Sets the Component's displacement in the y <span style="font-family: 宋体">direction</span></td> </tr> <tr> <td valign="top">Android:visibility</td> <td valign="top">Setvisibility (int)</td> <td valign="top">Sets whether the component is visible</td> </tr> </tbody> </table> <p><p>*drawable is an abstract class provided by Android that contains a large number of subclasses that can customize the appearance of the UI Component's Background.</p></p> <p><p>ViewGroup is an abstract class that actually uses his subclasses as containers, such as various layout containers, which control the distribution of the period subcomponents through the viewgroup.layoutparams, viewgroup.marginlayoutparams two inner classes.</p></p> <blockquote> <blockquote> <p>Viewgroup.layoutparams two properties</p> </blockquote> </blockquote> <p><p></p></p> <table style="color: #333" cellspacing="0" cellpadding="2" width="929" border="0"> <tbody> <tr> <td valign="top" width="372"><p align="center">XML properties</p></td> <td valign="top" width="555"><p align="center">Description</p></td> </tr> <tr> <td valign="top" width="372"><p style="margin-right: 0px" dir="ltr">Android:layout_height</p></td> <td valign="top" width="555">Specifies the layout height of the subassembly</td> </tr> <tr> <td valign="top" width="372">Android:layout_width</td> <td valign="top" width="555">Specifies the layout width of the subassembly</td> </tr> </tbody> </table> <p><p></p></p> <p><p><br>They all support the following three attribute values: fill_parent, match_parent, wrap_content</p></p> <blockquote> <blockquote> <p><br>Properties supported by Viewgroup.marginlayoutparams</p> </blockquote> </blockquote> <table style="color: #333" cellspacing="0" cellpadding="2" width="950" border="0"> <tbody> <tr> <td valign="top" width="200"><p align="center">XML properties</p></td> <td valign="top" width="240"><p align="center">Method</p></td> <td valign="top" width="508"><p align="center">Description</p></td> </tr> <tr> <td valign="top" width="200">Android:layout_marginbutton</td> <td valign="top" width="240">SetMargins (int,int,int,int)</td> <td valign="top" width="508">Specify the margins below the sub-component</td> </tr> <tr> <td valign="top" width="200">Android:layout_marginleft</td> <td valign="top" width="240"></td> <td valign="top" width="508"></td> </tr> <tr> <td valign="top" width="200">Android:layout_marginright</td> <td valign="top" width="240"></td> <td valign="top" width="508"></td> </tr> <tr> <td valign="top" width="200">Android:layout_margintop</td> <td valign="top" width="240"></td> <td valign="top" width="508"></td> </tr> </tbody> </table> <p><p></p></p>2. Using the XML layout to control the UI interface<p><p>The Java code displays the view in Activity:</p></p> <blockquote> <blockquote> <p>Setcontentview (r.layout.< resource File id>);</p> </blockquote> </blockquote> <p><p>Accessing UI components in Java code:</p></p> <blockquote> <blockquote> <p>Findviewbuid (r.id.<android.id attribute Value >);</p> </blockquote> </blockquote>3. Control the UI interface in code<pre><span style="color: #0000ff"><span style="color: #0000ff">protected</span></span> <span style="color: #0000ff"><span style="color: #0000ff">void</span></span><span style="color: #000000"><span style="color: #000000">onCreate (Bundle Savedinstancestate) {</span></span><span style="color: #0000ff"><span style="color: #0000ff">Super</span></span><span style="color: #000000"><span style="color: #000000">. OnCreate (savedinstancestate); </span></span><span style="color: #008000"><span style="color: #008000">//</span></span><span style="color: #008000"><span style="color: #008000">Setcontentview (r.layout.main); </span></span><span style="color: #008000"><span style="color: #008000">//</span></span><span style="color: #008000"><span style="color: #008000">Create a linear layout manager</span></span>LinearLayout layout =<span style="color: #0000ff"><span style="color: #0000ff">New</span></span>LinearLayout (<span style="color: #0000ff"><span style="color: #0000ff"></span> this</span><span style="color: #000000"><span style="color: #000000">); </span></span><span style="color: #008000"><span style="color: #008000">//</span></span><span style="color: #008000"><span style="color: #008000">set the activity display layout</span></span> <span style="color: #0000ff"><span style="color: #0000ff">Super</span></span><span style="color: #000000"><span style="color: #000000">. Setcontentview (layout); Layout.setorientation (linearlayout.vertical); </span></span><span style="color: #008000"><span style="color: #008000">//</span></span><span style="color: #008000"><span style="color: #008000">Create a TextView</span></span> <span style="color: #0000ff"><span style="color: #0000ff">Final</span></span>TextView show =<span style="color: #0000ff"><span style="color: #0000ff">New</span></span>TextView (<span style="color: #0000ff"><span style="color: #0000ff"></span> this</span><span style="color: #000000"><span style="color: #000000">); </span></span><span style="color: #008000"><span style="color: #008000">//</span></span><span style="color: #008000"><span style="color: #008000">Create a button</span></span>Button bn =<span style="color: #0000ff"><span style="color: #0000ff">New</span></span>Button (<span style="color: #0000ff"><span style="color: #0000ff"></span> this</span><span style="color: #000000"><span style="color: #000000">); Bn.settext (</span></span>"ok"<span style="color: #000000"><span style="color: #000000">); Bn.setlayoutparams (</span></span><span style="color: #0000ff"><span style="color: #0000ff">New</span></span><span style="color: #000000"><span style="color: #000000">Viewgroup.layoutparams (ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRA p_content)); </span></span><span style="color: #008000"><span style="color: #008000">//</span></span><span style="color: #008000"><span style="color: #008000">adding TextView to the layout container</span></span><span style="color: #000000"><span style="color: #000000">Layout.addview (show); </span></span><span style="color: #008000"><span style="color: #008000">//</span></span><span style="color: #008000"><span style="color: #008000">Add a button to layout</span></span><span style="color: #000000"><span style="color: #000000">Layout.addview (bn); </span></span><span style="color: #008000"><span style="color: #008000">//</span></span><span style="color: #008000"><span style="color: #008000">binding an event listener for a button</span></span>Bn.setonclicklistener (<span style="color: #0000ff"><span style="color: #0000ff">New</span></span><span style="color: #000000"><span style="color: #000000">Onclicklistener () {@Override</span></span><span style="color: #0000ff"><span style="color: #0000ff"></span> public</span> <span style="color: #0000ff"><span style="color: #0000ff">void</span></span><span style="color: #000000"><span style="color: #000000">OnClick (View V) {</span></span><span style="color: #008000"><span style="color: #008000">//</span></span><span style="color: #008000"><span style="color: #008000">TODO auto-generated method stubs</span></span>Show.settext ("click success"<span style="color: #000000"><span style="color: #000000">); } }); }</span></span></pre><p><p>The This above is a context object that represents an API to access global information for the Android app Environment. He is an abstract class, and the Activity and service inherit the Context.</p></p>4. General combination of XML layout and code blending control UI Interface<p><p>In this case, it is customary to place components with smaller changes and more fixed behavior in the XML layout file, while those that are more variable and more complex in behavior control are given Java code to Manage.</p></p>5. Develop a custom view<p><p>When the Android system provides a UI component that is not sufficient to meet the requirements of the project, we can derive the custom component by inheriting the VIEW.</p></p> <blockquote> <blockquote> <p>First define a subclass that inherits the view base class, and then override one or more methods of the view class.<br></p> </blockquote> </blockquote> <ul> <ul> <li>Constructor: when view is instantiated or loaded and the interface is built, the constructor will need to be called</li> <li>Onfinishinflate () triggers when all child controls in view are mapped to XML</li> <li>Onmeasure (int, Int) Determines the size of all child elements</li> <li>OnLayout (boolean, int, int, int, Int) triggered when view allocates the size and position of all child elements</li> <li>onsizechanged (int, int, int, Int) triggered when the size of the view changes</li> <li>OnDraw (Canvas) View details of rendered content</li> <li>OnKeyDown (int, Keyevent) has a button pressed and then triggered</li> <li>OnKeyUp (int, keyevent) Triggers when the button is pressed and bounced</li> <li>Ontrackballevent (motionevent) trackball Event</li> <li>Ontouchevent (motionevent) Touch Screen Event</li> <li>onfocuschanged (boolean, int, Rect) triggers when view Gets or loses focus</li> <li>Onwindowfocuschanged (boolean) triggers when a window contains a view that gets or loses focus</li> <li>Onattachedtowindow () triggers when view is attached to a window</li> <li>Ondetachedfromwindow () triggers when view leaves an attached window, Android123 hints that the method and Onattachedtowindow () are Reversed.</li> <li>Onwindowvisibilitychanged (int) Triggers when the visible view that is contained in the window has changed</li> </ul> </ul><p><p>02_android Application Interface Programming _01_ View component</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.