Namespace and custom attributes of XML in Android

Posted by returnButton on Sat, 20 Nov 2021 03:13:30 +0100

The namespace stores a collection of specific attributes,

android, tools, app (custom namespace)

1,android

xmlns:android="http://schemas.android.com/apk/res/android

xmlns: that is, xml namespace, which declares that we are going to start defining a namespace  

android: called namespace prefix, it is the name of the namespace  

http://schemas.android.com/apk/res/android : This looks like a URL, but this address is inaccessible. In fact, this is a URI (Uniform Resource Identifier), so its value is fixed (equivalent to a constant).

Prompt you to enter what can also be understood as a syntax file.

In this layout, as long as the attribute starting with android: refers to the attribute in the namespace,

2,tools

xmlns:tools="http://schemas.android.com/tools"

2.1. tools only works in the development stage

It only works in the development stage. When the app is packaged, all the tools attributes will be discarded!

2.2. Tools: view the Activity layout effect in context development

tools:context="com.littlehan.myapplication.MainActivity"

By adding this line of code to the layout, you can see the effect of binding the theme with MainActivity in the design view.

2.3. Tools: view the fragment layout effect during layout development

tools:layout=@layout/yourfragmentlayoutname 

In this way, your fragment layout will be previewed on the specified main layout

3. Custom namespace

If you use DataBinding, you will use the app attribute in xml. In fact, this is a custom namespace.

xmlns:app="http://schemas.android.com/apk/res-auto"

In fact, it can also be written as follows:  

xmlns:app="http://schemas.android.com/apk/res/ "Full package name"  

In general, custom namespaces are often inseparable from custom views. When Android's built-in controls can't meet the requirements, you can draw some views yourself. To add custom properties to a custom View, you need to create a custom namespace.

The process of customizing the View can be divided into the following steps:

3.1. Inherit View class

Create a class named CustomTextView to inherit View
(View is the parent class of all views) and implement its three construction methods

public class CustomTextView extends View {
    private Paint mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);//paint brush
    public CustomTextView(Context context) {
        super(context);
    }
    public CustomTextView(Context context, AttributeSet attrs){
        this(context, attrs, 0);//Note that it is not super(context,attrs,0);
    }
    public CustomTextView(Context context, AttributeSet attrs, int defStyleAttr){
        super(context,attrs,defStyleAttr);
    }
}

2.3. Tools: view the fragment layout effect during layout development3.2. Use custom layout

XML creates custom attributes and parses attributes in custom views

Bring custom controls into layout

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#ffffff"
    >
<com.littlehan.customtextview.CustomTextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />
</RelativeLayout>

3.3. User defined attributes

Create a new xml file named attrs in the root directory of values to define attributes (custom attributes are attributes in the custom namespace)

Name defines the name of the attribute  

format defines the type of the property

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="CustomTextView">
    <attr name="customColor" format="color"/>
    <attr name="customText" format="string"/>
    </declare-styleable>
</resources>

3.4. Parsing attributes

Resolve these properties in CustomeTextView

public class CustomTextView extends View {
    private int mColor = Color.RED;//The default is red
    private String mText="I am a Custom TextView";//The text is displayed by default
    private Paint mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);//paint brush
    public CustomTextView(Context context) {
        super(context);
//        init();
    }
    public CustomTextView(Context context, AttributeSet attrs){
        this(context, attrs, 0);//Note that it is not super(context,attrs,0);
        init();
    }
    public CustomTextView(Context context, AttributeSet attrs, int defStyleAttr){//Resolve custom properties
        super(context,attrs,defStyleAttr);
        TypedArray typedArray = context.obtainStyledAttributes(attrs,R.styleable.CustomTextView);
        mColor = typedArray.getColor(R.styleable.CustomTextView_customColor, Color.RED);
//        If there is no judgment, the app will crash when it loads the property without specifying the property
        if(typedArray.getText(R.styleable.CustomTextView_customText) != null ){
            mText = typedArray.getText(R.styleable.CustomTextView_customText).toString();
        }
        typedArray.recycle();//Release resources
        init();
    }
    private void init(){
        mPaint.setColor(mColor);// Add color to brush
    }
 
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.drawText(mText, 100, 100, mPaint);
    }
}

3.5. Use custom attributes

To use custom attributes, you need to customize the attribute namespace and insert such a line of code under the root element of the layout file:  

xmlns:app="http://schemas.android.com/apk/res-auto"

Topics: Android xml