Detailed explanation of AttributeSet case of Android

  • 2021-12-19 06:37:24
  • OfStack


public interface AttributeSet {
    /**
     * Returns the number of attributes available in the set.
     * 
     * @return A positive integer, or 0 if the set is empty.
     */
    public int getAttributeCount();

    /**
     * Returns the name of the specified attribute.
     * 
     * @param index Index of the desired attribute, 0...count-1.
     * 
     * @return A String containing the name of the attribute, or null if the
     *         attribute cannot be found.
     */
    public String getAttributeName(int index);

    /**
     * Returns the value of the specified attribute as a string representation.
     * 
     * @param index Index of the desired attribute, 0...count-1.
     * 
     * @return A String containing the value of the attribute, or null if the
     *         attribute cannot be found.
     */
    public String getAttributeValue(int index);

    /**
     * Returns the value of the specified attribute as a string representation.
     * The lookup is performed using the attribute name.
     * 
     * @param namespace The namespace of the attribute to get the value from.
     * @param name The name of the attribute to get the value from.
     * 
     * @return A String containing the value of the attribute, or null if the
     *         attribute cannot be found.
     */
    public String getAttributeValue(String namespace, String name);

Look at the source code of AttributeSet and you will find that it is an interface. What interface is it?

People familiar with XML parsing know that there is AttributeSet in XML parsing, and XML parsing takes out the data corresponding to nodes according to nodes.

In Android, the layout file we write is in the form of XML, so that's why every time we customize View, the constructor has AttributeSet.

The explanation given by SDK is as follows:


A collection of attributes, as found associated with a tag in an XML document. Often you will not want to use this interface directly, instead passing it to Resources.Theme.obtainStyledAttributes() which will take care of parsing the attributes for you. In particular, the Resources API will convert resource references (attribute values such as "@string/my_label" in the original XML) to the desired type for you; if you use AttributeSet directly then you will need to manually check for resource references (with getAttributeResourceValue(int, int)) and do the resource lookup yourself if needed. Direct use of AttributeSet also prevents the application of themes and styles when retrieving attribute values.

This interface provides an efficient mechanism for retrieving data from compiled XML files, which can be retrieved for a particular XmlPullParser through Xml.asAttributeSet(). Normally this will return an implementation of the interface that works on top of a generic XmlPullParser, however it is more useful in conjunction with compiled XML resources:

When we customize View, how do we use AttributeSet?

1 In general, we create a new attrs folder under values


<declare-styleable name="MyView">  
    <attr name="textColor" format="color"/>  
    <attr name="textSize" format="dimension"/>  
</declare-styleable>

The layout file is as follows:


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:myapp="http://schemas.android.com/apk/res/com.example.androidtest"
    android:layout_width="match_parent"
    android:orientation="vertical"
    android:background="@android:color/black"
    android:layout_height="match_parent">
    
    <com.example.androidtest.MyView
        android:layout_width="fill_parent"  
        android:layout_height="wrap_content"    
        myapp:textColor="#FFFFFFFF"    
        myapp:textSize="62dp"  
        ></com.example.androidtest.MyView>

</LinearLayout>

Custom View sample code:


public class MyView extends TextView {

    public MyView(Context context, AttributeSet attrs) {
        super(context, attrs);
        // TODO Auto-generated constructor stub
        
        TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.MyView);
        int textColor = array.getColor(R.styleable.MyView_textColor, 0XFF00FF00);
        float textSize = array.getDimension(R.styleable.MyView_textSize, 36);
        setTextColor(textColor);
        setTextSize(textSize);
        setText("22222222222");
        
        array.recycle();
    }

    public MyView(Context context) {
        super(context);
        // TODO Auto-generated constructor stub
    }

Related articles: