<span id="Label3"></p><p><p>This paper mainly introduces the main elements involved in application lock, including resources, components, event processing, application style, data adaptation, etc.</p></p><p><p>1. Layout Files</p></p><p><p>Layout files are typically composed of a root layout and a sub-layout, and in the application of the layout file, different controls and target environments are Involved. The layout file looks simple, but it requires a lot of skill and experience, This section only covers layout loading, density, and the use of some special tags.</p></p><p><p>(1) Load Layout</p></p><p><p>The Setcontextview method can be used to load the layout file implicitly, and if you want to load the layout file in the display, you can get the Layout_inflater_ by using the Getlayoutinflater method in Activity. Service services instantiate the layout file. alternatively, you can get a copy of the existing Layoutinflater object to instantiate the layout file.</p></p><p><p>Instantiate the layout file with the Getlayoutinflater method: View demo=getlayoutinflater (). Inflate (r.layout.demo, null);</p></p><p><p>Instantiating a layout file through System services: layoutinflater inflater= (layoutinflater) context.getSystemServiceContext.LAYOUT_INFLATER_SERVICE); View Demo=inflater. Inflater (r.layout.demo, null);</p></p><p><p>Instantiating a layout file from a copy of an existing Layoutinflater object: layoutinflater inflater=from (context); View Demo=inflater. Inflater (r.layout.demo, null);</p></p><p><p>(2) the logic of density</p></p><p><p>When building a layout file, It naturally involves the problem of pixels, in android, there are several units, such as dp, px, dip, which are most commonly used dip.</p></p><p><p>Dip settings are independent of resolution, but are related to screen Density. By default, the density of ldpi is 120, the coefficient is 0.75;mdpi density is 160, the coefficient is 1.0;hdpi density is 240, the coefficient is 1.5;xhdpi density is 320, the coefficient is 2.0. The so-called density is the number of pixels per square Inch.</p></p><p><p>The relationship between PX and dip is as follows: px= (int) (dip*density+0.5f)</p></p><p><p>(3) Special Label</p></p><p><p>In the Android layout file, there are several special tags in addition to the normal UI control tags, which are viewstub, reuqestfocus, merge, and Include.</p></p><p><p>1) viewstub Label</p></p><p><p>The viewstub tag is actually a special control that, by default, contains controls that are not visible and do not occupy any memory space, and developers can load the layout that the viewstub tag contains through setvisibility and Inflater.</p></p><p><p>The Viewstub label contains layouts that are external layout files that are referenced through the android:layout Property.</p></p><p><p>2) Requestfocus Label</p></p><p><p>Requestfocus label 0 enables the corresponding UI control to have the focus, when used, inside the UI control Label.</p></p><p><p>3) Merge Label</p></p><p><p>The merge tag plays an important role in optimizing the UI layout and can reduce extra or extra levels. In the application, the merge label is primarily used to optimize the root layout or to reduce reference redundancy when using viewstub tags and include tags. Note that the merge label can only be used as the root layout of the layout file, and if you want to load the layout file with the merge tag as the root layout through the layout loader (layoutinflater), you need to use the view inflate (int resource, ViewGroup root, Boolean attachtoroot) specifies a viewgroup as its container. and to set Attachtoroot to True.</p></p><p><p>4) include tags</p></p><p><p>The include tag acts like the Include keyword in c, which refers to an external layout file and acts as a reusable layout file.</p></p><p><p>(4) Allocation of available space</p></p><p><p>In a layout file, how to make the written layout file as adaptable as possible to a different target Environment. While Android has made a number of considerations for achieving the adaptation of the target environment, it is possible to pinpoint the resolution, density, screen, and so on for the layout file, but nobody likes to write multiple layout files for the same interface, because such maintenance costs are too great, so try to make the layout file structure as much as Possible.</p></p><p><p>The Author's experience is to use the layout controls such as LinearLayout and Relativelayout as flexibly as Possible. For linearlayout, The key is the flexible use of its android:layout_weight properties, relativelayout is relatively simple.</p></p><p><p>2. Value file</p></p><p><p>A value file is a required resource file for Android apps that is responsible for the UI configuration and text display of your app. Value files include string files, string array files, configuration files, integer array files, dimension files, property files, color files, style files, subject files, descriptor files, and so on, where the English directory is Res\value.</p></p><p><p>(1) string file</p></p><p><p>String is the most commonly used value resource, its common setting method: <string name= "edit_event_from_lable" >From</string></p></p><p><p>If the string resource does not have to be localized, you need to set its Translatable property to false, setting the method: <string name= "voice_mode_off" translatable= "false" >2</ String></p></p><p><p>As the resource is loaded, it is loaded with the ID of the resource, and Android does not allow the same ID to appear in the same namespace.</p></p><p><p>In android, resource sub-system resources and application resources, the two resources in the application of the way the reference is slightly different. system resources are not all developed for the application layer, and the system resources open to the application layer are defined in the public.xml of the frameworks\base\core\res\res\values\ directory. How to load system resources that are not developed by the application Tier: getString (com.android.internal.r.string.using)</p></p><p><p>In android, file effects currently only support bold and italic two, to achieve the underlined function, you need to borrow the group Html syntax, the implementation method: Textview.settext ("<u>" +hellostr+ " </u> "))</p></p><p><p>Language switching is done through the resource manager, the implementation method: Resource resource=getresources (); Configuration config=resource.getconfiguration (); Displaymetrics Dm=resource.getdisplaymetrice (); config.locale=locale.simplified_chinese; Resource.updateconfiguration (config, dm);</p></p><p><p>(2) string Array file</p></p><p><p>String arrays are typically used for scenarios such as Drop-down boxes, and you can set the Translatable property of a string array to False if you do not want the string array to change as the locale Changes.</p></p><p><p>(3) configuration file</p></p><p><p>Profiles are not commonly used in application development, typically for scenarios such as app settings, configuration file implementations:</p></p><p><p><resources></p></p><p><p><bool name= "config_sf_limitedalpha" >false</bool></p></p><p><p><integer name= "config_shortanimtime" >150</integer></p></p><p><p></resources></p></p><p><p>How to load a configuration file in Java: mlimitedalphacompositing=context.getresource (). Getboolean (com.android.internal.r.bool.config_ sf_limitedalpha);</p></p><p><p>(4) Integer Data file</p></p><p><p>Arrays of integers and string data are used in a similar way.</p></p><p><p>How to load an integer array in java: int[] tmparray=getresource (). getintarray (com.android.internal.r.array.config_ virtualkeyvibepattern);</p></p><p><p>(5) Dimension File</p></p><p><p>How to load dimension files in Java: Int w=getresource (). getdimensionpixelsize (com.android.internal.r.dimen.thumbnail_width);</p></p><p><p>(6) Properties File</p></p><p><p>In android, a property file is usually made up of multiple tags,</p></p><p><p>Declare-styleable tags are used to declare an attribute group, and an attribute group can consist of multiple properties.</p></p><p><p>Methods for loading attribute groups: typearray a=context.obtainstyledattributes (attrs, com.android.internal.r.styleable.animation); A.recycle ();</p></p><p><p>The Eat-comment label is used to declare the content on the label to be a Comment. The attr tag is used to declare properties. A property consists of the property name and the attribute format two Parts.</p></p><p><p>The implementation of preference values is divided into enum, flag two types, where flag indicates a binary representation of preference Values.</p></p><p><p>(7) Color File</p></p><p><p>The color settings file is typically placed in the resource directory of the framework layer, referencing the color attributes instead of displaying the claims at the application level, which facilitates UI consistency at the system Level. In the declaration of color, the format of the time ARGB used</p></p><p><p><resource></p></p><p><p><color name= "magic_flame" > #00ffffff </color></p></p><p><p></resource></p></p><p><p>How color settings are loaded in Java: setbackgroundcolor (getresources (). getColor (r.color.candidate_background));</p></p><p><p>(8) Style File</p></p><p><p>A good application in addition to the technical design should be elegant (that is, the code has high readability, extensibility, reusability, performance), The user experience is also very important (including the wording and look and feel and interactive design), the style of design determines the user experience of the text and the effect of the Picture.</p></p><p><p>(9) Theme Files</p></p><p><p>Theme files are commonly used at the framework level, and it is a good idea to maintain the same topic as a basic UI requirement for vendors with differentiated work, and to define a unified style file for all company product References.</p></p><p><p>(10) identifier File</p></p><p><p>Identifier files are uncommon for application layer developers, but this does not mean that the developer is completely inaccessible. The use of system-provided listview, tab, and so on will be Involved.</p></p><p><p>3. Create a menu</p></p><p><p>Depending on the application scenario, the menu can be divided into Options menu (optionsmenu), Context menu, submenu (submenu) and so on 3 kinds, the option menu is the most common menu (press the menu key pop-up menus are the options menu), Context menus are typically used in scenarios such as Lists.</p></p><p><p>In addition to optionsmenu, submenu, ContextMenu outside. Android also often involves classes such as menuitem, menuinflater, and so on when building menus. Menuinflater the parser used to load the XML resource file is Xmlpullparser. For performance reasons, currently menuinflater does not support loading of unprocessed XML resource Files.</p></p><p><p>The context menu and submenu do not support Icons. The concept of menu groups and shortcut keys is not commonly used in the current touchscreen-based smart Terminal.</p></p><p><p>(1) Options Menu</p></p><p><p></p></p><p><p>Android Resource Chapter</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.