Summary of some less commonly used XML file attributes in Android

Source: Internet
Author: User
Tags rfc

Summary of some less commonly used XML file attributes in Android
Preface

Some attributes that may not be frequently used in recent projects are used in XML, which may involve AndroidManifest. xml, Layout file Layout, or specific controls!

AndroidManifest. xml

Add tags in the AndroidManifest. xml file
,
Xxx. xxx. xxx is the third-party library package name. If multiple libraries exist, separate them with commas.In this way, the minimum version limit can be ignored when AndroidManifest. xml in the project is merged with AndroidManifest. xml in a third-party library.


Whether to back up application data. The default value is true. When backing up data, its data will be backed up. If it is set to false, the application data will never be backed up, even if the entire system is backed up.


Android now provides some APIs that allow you to build a more elegant user interface for changing the layout direction. These interfaces support the language and reading direction from right to left (RTL. For example, Arabic and Hebrew.
To make your application support RTL layout, you must set the android: supportsRtl attribute of the element to "true" in the manifest file ". Once enabled, the system enables various RTL APIs to display your applications in the RTL layout.
If this attribute is used, pay attention to the following issues:
1. The minimum SDK version is 17.
2. Change the layout attribute from left to right to the layout attribute from start to end.


By default, the AS Gradle plug-in enables Manifest Merger Tool. If the same attributes AS the main project are defined in the Library project (for example, the default android: icon and android: theme ), the merge operation fails and an error is returned.
In this case, you need to add the attribute above in manifest. xml.

EnabledIndicates whether the service can be instantiated by the system. True if yes; otherwise, false. The default value is true.An element has its own enabled attributes that can be applied to all application components, including services. If this service is enabledAndAll attributes must be true (both of them are default values ). If one of them is false, the service will be disabled; it will not be instantiated.

ExportedAttribute indicates whether other application components can call or interact with the service. If yes, the value is true; otherwise, the value is false. When the value is false, only components of the same application or applications with the same user ID can start the service or bind it.
The default value depends on whether the service contains intent filters. The missing filter means that it can only be called by specifying its exact class name. This means that this service can only be used inside the application (because other applications do not know the class name ). Therefore, in this case, the default value is false. On the other hand, at least one filter means that this service can be used externally. Therefore, the default value is true.

 

The excludeFromRecents attribute indicates that the control is not displayed in the recent list. True is not displayed; false is displayed, default is.
Tools: ignore = "ExportedActivity" indicates that all applications are allowed to use it.

7. data tag under intent-filter

<code class=" hljs xml"><data android:mimetype="*/*">
<data android:scheme="file">
<data android:host="*">
<data android:path="*">
<data android:pathprefix="*">
<data android:port="*">
<data android:pathpattern=".*..*..*..*..*.vpn"></data></data></data></data></data></data></data></code>

This label element is used to add a data specification to an Intent filter. A data specification can be a data type (mimeType attribute) or a data location identifier (URI ), it can also be a data type and a data location identifier (URI ). A uri (in the following format) is divided into several independent attributes to be specified separately:
scheme://host:port/path or pathPrefix or pathPattern
These attributes are optional but also dependent on each other. If the scheme attribute is not specified for the Intent filter, all other URI attributes will be ignored. If the host attribute is not specified for the filter, the port attribute and all path attributes are ignored.
All data elements contained in the same intent-filter element only apply to this filter.

MimeType
Multi-purpose Internet Mail Extension (MIME, Multipurpose Internet Mail Extensions) is used to tell the Android system the types of files that the Activity can process.
Format: [type]/[subtype]
Type has the following format:

Text: it is used to represent text information in a standardized way. Text messages can be in multiple character sets and formats;
Multipart: used to connect multiple parts of the message body to form a message. These parts can be different types of data;
Application: used to transfer application data or binary data;
Message: used to package an e-mail message;
Image: used to transfer static picture data;
Audio: used to transmit audio or audio data;
Video: used to transmit dynamic image data, which can be video data format together with audio editing.

Subtype is used to specify the type details. The set of content-type/subtype pairs and related parameters will increase over time. To ensure that these values are developed in an orderly and open state, MIME uses Internet Assigned Numbers Authority (IANA) as the central registration mechanism to manage these values. Common subtype values are as follows:

Text / plain
Text / HTML (HTML document)
Application / XHTML + XML (XHTML document)
Image / GIF (GIF image)
Image / jpeg (JPEG image) [in PHP: image / pjpeg]
Image / PNG (PNG image) [image / x-png in PHP]
Video / MPEG (MPEG animation)
Application / octet stream (arbitrary binary data)
Application / PDF (PDF document)
Application / msword (Microsoft Word file)
Message / rfc822 (RFC 822 form)
Multipart / alternative (HTML form and plain text form of HTML mail, the same content is expressed in different forms)
Application / x-www-form-urlencoded (form submitted using HTTP's post method)
Multipart / form data (the same as above, but mainly used when the form is submitted and the file is uploaded)
Host

Host
This attribute defines the host part authorized by the URI, unless the filter is also specifiedThe scheme attribute of the element. Otherwise, this attribute is meaningless.
Note: In the Android framework, host name matching is case sensitive, which is different from RFC format. Therefore, you must always use lowercase letters to specify the host name.

Path/pathPrefix/pathPattern
The three attributes are used to specify the URL of the URI. The Path attribute specifies a complete Path, which matches the Path in the Intent object. The PathPrefix attribute specifies only part of the path, which matches the initial part of the path in the Intent object. The pathPattern attribute specifies a complete path to match the path in the Intent object, but this path can contain the following wildcards:
1. asterisks*Wildcard. The asterisk in the path represents the character before any number of asterisks, sucha*, Can be associated with a, aa, aaa, aaaa ,... String matching.
2. Combination of dots and asterisks.*Wildcard, which can be matched with any string, such as. * html, which can be followed by abchhtml?chtml=html#dssf.html ,... .

Because when the system reads a string in XML’\’The symbol is a mandatory escape character, so it needs to be escaped twice. For example, symbol”*”To be written”\\*”, Symbol’\’To be written”\\\\”. This is basically the same as writing in Java code.

Port
This attribute is used to define the port section of the URI authorization. This attribute makes sense only when the scheme and host attributes are specified for the filter.

Scheme
This attribute is used to set the scheme part of the URI. It is the most basic attribute set for a specified URI. At least a scheme attribute must be set for the filter. Otherwise, other URI attributes are meaningless.
The scheme property value does not end with the ":" symbol (such as http, rather than http :)
If the filter has a data type (the mimeType attribute is set) but the scheme attribute is not set, the system assumes that the scheme is content: and file:
Note: In the Android framework, scheme is highly sensitive to lower case, which is different from RFC format. Therefore, you must always use lower-case letters to specify scheme.

BytesTag
Meta-data, like its name, is mainly used to define configuration values related to some components.
According to the official definition, metadata is a group of name-value pair for the parent component. Therefore, the corresponding meta-data element should be defined in the corresponding component. To use metadata in an activity, meta-data must be defined in the activity declaration of AndroidManifest. xml.
Usage:

 

Then we can get our value in the Code:

// apply elements to the Activity.
ActivityInfo info = this. GetPackageManager (). GetActivityInfo (getComponentName (), PackageManager. The GET_META_DATA);
Info. MetaData. Get string (" meta_name ");

// apply elements in application.
ApplicationInfo appInfo = this. GetPackageManager (). GetApplicationInfo (getPackageName (), PackageManager. The GET_META_DATA);
Meta_name appInfo. MetaData. Get string (" ");

// apply the element to the service.
The ComponentName cn = new the ComponentName (this, MetaDataService class);
ServiceInfo info = this.getpackagemanager ().getserviceinfo (cn, packagemanager.get_meta_data);
Info. MetaData. Get string (" meta_name ");

// applies elements in the receiver.
ComponentName cn = new ComponentName(context, MetaDataReceiver. Class);
ActivityInfo info = context. GetPackageManager (). GetReceiverInfo (cn, PackageManager. GET_META_DATA);
Info. MetaData. Get string (" meta_name ");
Layout. xml ① add tools: context to the layout or control
 

tools:context="activity name"This statement will not be packaged into the APK. Only the Layout Editor of ADT is inSet the corresponding rendering context in your current Layout FileThe rendering context of your current Layout is the activity corresponding to the activity name. If Theme is set in the manifest file, then, the Layout Editor of the ADT will render your current Layout based on the Theme. That is to say, if the MainActivity you set has a Theme. light (others can also be used), then Theme is what you see in the visual layout manager. light. It is only used to show you what you see is what you get.

② Abstract layout labels

Use abstract layout labels (Include, viewstub, merge) Mainly to optimize the layout! Remove unnecessary nesting and View nodes, reduce unnecessary infalte and other Layout adjustable advantages.

Tag

The include label is often used to extract the public part of the layout for sharing by other layout to implement modular layout, which provides great convenience in layout compilation.
Usage:

The only attribute required by the include tag is the layout attribute, which specifies the layout file to be included. You can define the android: id and android: layout _ * attributes to overwrite the corresponding attribute values of the introduced layout root node.

Tag

The viewstub label can be used to introduce an external layout like the include label. The difference is that the layout introduced by viewstub will not expand by default, that is, it will neither occupy the display nor the position, therefore, cpu and memory are saved during layout parsing.
Viewstub is often used to introduce the la s that are not displayed by default and only displayed under special circumstances, such as the progress layout, the refreshing layout displayed when the network fails, and the layout prompts when information errors occur.
Usage:

 

Network_error.xml is the layout that must be displayed only when a network error occurs. It is not parsed by default.
We can use the following two methods to achieve the same effect:

((ViewStub) findViewById(R.id.layout_error)).setVisibility(View.VISIBLE);  
// or 
View importPanel = ((ViewStub) findViewById(R.id.layout_error)).inflate();
Tag

When include is used, too many layout nesting and unnecessary layout nodes may be added, resulting in slow resolution, you can view unnecessary nodes and nesting through hierarchy viewer or set-> developer options-> display layout boundaries. Merge labels play an important role in UI structure optimization. They can delete redundant levels and optimize the UI.
Merge is mostly used to replace FrameLayout or remove unnecessary view groups in the view hierarchy when one layout contains another.

The merge tag can be used in two typical scenarios:
A.The layout top node is FrameLayout and does not need to set attributes such as background or padding. You can use merge instead, because the parent view of the Activity content view is a FrameLayout, so you can use merge to eliminate only one.
B.When a layout is included by other la s as a sub-layout, merge is used as the top node of the layout. As a result, the top node is automatically ignored when it is introduced, merge all its subnodes into the main layout.

Usage:

 Widget 

① TextView

Android: textAppearance = "? Android: attr/XXXX"
       Android: ellipsize = "start"
       Android: textStyle = "bold"

② EditText

android:imeActionId="@+id/login"
android:imeActionLabel="@string/sign_in"
android:imeOptions="actionUnspecified"

These three attributes are used to set the function of the carriage return key in the lower right corner of the soft keyboard, and then rewrite the oneditexception () method in the Code. The actionId In the parameter corresponds to the value set in the imeOption attribute.

Android: textCursorDrawable = "@ null"


Related Article

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.