Android's custom view and custom attributes

I came across an interview question some time ago. It is not difficult to say it is difficult. It is simple but I have never touched it before. It may be that my foundation is too weak. The interviewer asked this question: Customizing the view is very simple, but customizing the view is very difficult. How to add properties? At that time, I didn't answer, and I was blinded! Later, I came back to the Internet and checked the custom properties of the custom view, only to know that it was so simple.

We know that a custom view can be implemented by inheriting the view and then overriding the onMeasure, onLayout, and onDraw methods, but how to add your own attributes to the custom view?
Let's use a simple demo example to explain:

First, the custom view code

package cn.jhsys.defineviewdemo;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.View;

public class MyView extends View {

    public MyView(Context context) {
        super(context);
    }

    public MyView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(attrs);
    }

    public MyView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(attrs);
    }

    public void init(AttributeSet attrs){  

        TypedArray t = getContext().obtainStyledAttributes(attrs,R.styleable.MyElement);  

        String textValue = t.getString(R.styleable.MyElement_textValue);  
        float textSize = t.getDimension(R.styleable.MyElement_textSize, 36);  
        int textColor = t.getColor(R.styleable.MyElement_textColor, 0xff000000);  

        System.out.println("textValue=" + textValue + ",textSize=" + textSize
                + ",textColor=" + textColor);
    }

}

2. Layout file

<LinearLayout     xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
 xmlns:my="http://schemas.android.com/apk/res/cn.jhsys.defineviewdemo"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <cn.jhsys.defineviewdemo.MyView 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        my:textValue="你好1"
        my:textColor="#ff00"/>
    <cn.jhsys.defineviewdemo.MyView 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        my:textValue="你好2"
        my:textColor="#22ff"/>

</LinearLayout>

There are two points to pay attention to here: 1. A new namespace is added to the layout file, xmlns:my=”http://schemas.android.com/apk/res/cn.jhsys.defineviewdemo”, the name my can be changed arbitrarily , followed by the package name of the application; 2. When referencing a custom view, it needs to be accompanied by the package name.

Three, attrs.xml file

You need to create a new attrs.xml file under values, you can customize the content

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="MyElement">  
        <attr name="textColor" format="color" />  
        <attr name="textSize" format="dimension" />  
        <attr name="textValue" format="string" />  
    </declare-styleable>
</resources>

As for how to add custom attributes to custom views, this is simply the case.

Demo download address: http://download.csdn.net/detail/wufeiqing/9505682

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325850701&siteId=291194637