Functions and customization of Android xmlns

Source: Internet
Author: User

Xmlns: Android = "http://schemas.android.com/apk/res/androidis

This is the namespace of XML. With this namespace, you can use Alt +/as a prompt to prompt you about what you enter, what you should not enter, what is right, and what is wrong, it can also be understood as a syntax file. Or a syntax judge or something.
This is mainly used to identify the attributes of the control during running. If you write an error, there will be no problems, but there will be problems during running, the prompt is that you do not specify the width or anything. This does not require Internet connection.

The Android-defined xmlns is actually very simple. The syntax rules are as follows:

Add xmlns: prefix = http://schemas.android.com/apk/res/your application package path to the XML layout file that uses the custom view.

The following is a simple example:

Structure:

Myview. Java

Package kexc. myview;

Import Android. content. context;
Import Android. content. res. typedarray;
Import Android. util. attributeset;
Import Android. widget. textview;
Public class myview extends textview {
Private string mstring = "Welcome to kesion's blog ";

Public myview (context, attributeset attrs ){
Super (context, attrs );
Typedarray A = context. obtainstyledattributes (attrs,
R. styleable. myview );
Int textcolor = A. getcolor (R. styleable. myview_textcolor,
0 xffffffff );
Float textsize = A. getdimension (R. styleable. myview_textsize, 36 );
Mstring = A. getstring (R. styleable. myview_title );
Settext (mstring );
Settextsize (textsize );
Settextcolor (textcolor );
}
}

 

Main. xml

<? XML version = "1.0" encoding = "UTF-8"?>
<Linearlayout
Xmlns: Android = "http://schemas.android.com/apk/res/android"
Xmlns: test = "http://schemas.android.com/apk/res/kexc.myView"
Android: Orientation = "vertical"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent">
<Textview
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: text = "@ string/hello"
/>
<Kexc. myview. myview
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
Test: Title = "wo Shi text"
Test: textsize = "20px"
Test: textcolor = "# fff"
/>
</Linearlayout>

 

Attribute file value/attrs. xml

<? XML version = "1.0" encoding = "UTF-8"?>
<Resources>
<Declare-styleable name = "myview">
<ATTR name = "textcolor" format = "color"/>
<ATTR name = "textsize" format = "dimension"/>
<ATTR name = "title" format = "string"/>
</Declare-styleable>
</Resources>

Running result:

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.