Summary of Custom Combination Controls

Please indicate the source for the summary of the custom combination control
reprint: http://renyuan-1991.iteye.com/blog/2306381
Step 1: Add the attrs.xml file in Values, first figure out how to use atts.xml, below is a complete properties file. The properties of the control actually provide us with some information. When we distinguish this property, we don't need to think about what it can do, just know what it can bring. For example, format is an attribute of type string. We only need to know that this attribute can provide us with a string of type string. What it is used for depends on personal preference.
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="GeneralTitle">
        <!-- This property is used to get String information-->
        <attr name="name" format="string"/>
        <!-- Provide a reference to the resource file, such as: @drawable/image ID -->
        <attr name="name" format="reference"/>
        <!-- Provide color value such as: #00FF00-->
        <attr name="name" format="color"/>
        <!-- Provide a boolean value such as: true -->
        <attr name="name" format="boolean"/>
        <!-- Provide a size value such as: 42dip -->
        <attr name="name" format="dimension"/>
        <!-- Provide a floating-point value such as: 2.2 -->
        <attr name="name" format="float"/>
        <!-- Provide an integer value such as: 2 -->
        <attr name="name" format="integer"/>
        <!-- Provide a percentage such as: 100% -->
        <attr name="name" format="fraction"/>
        <!-- Definition of enumeration value -->
        <attr name="orientation">
            <enum name="horizontal" value="0" />
            <enum name="vertical" value="1" />
        </attr>
        <!-- bitwise OR (this is never used...) -->
        <attr name="windowSoftInputMode">
            <flag name = "stateUnspecified" value = "0" />
            <flag name = "stateUnchanged" value = "1" />
            <flag name = "stateHidden" value = "2" />
            <flag name = "stateAlwaysHidden" value = "3" />
            <flag name = "stateVisible" value = "4" />
            <flag name = "stateAlwaysVisible" value = "5" />
            <flag name = "adjustUnspecified" value = "0x00" />
            <flag name = "adjustResize" value = "0x10" />
            <flag name = "adjustPan" value = "0x20" />
            <flag name = "adjustNothing" value = "0x30" />
        </attr>
    </declare-styleable>
</resources>

When defining the properties of Activity, this property is used in bit or operation, such as:
android:windowSoftInputMode = "stateUnspecified | stateUnchanged | stateHidden"

The advantage of this is to reduce data transmission, and only need to pass an int value.
When defining a property, you can specify multiple types of values, such as:
<attr name = "background" format = "reference|color" />

use:
android:background = "@drawable/图片ID|#00FF00"

The second step: the realization of
the custom custom space generally has three constructors, and these three constructors have some differences:
the constructor of one parameter:
    called when a View is instantiated in Code.
Constructor with two arguments:
    called when defined in xml.
Constructor with three parameters:
    In general, we need to display the call and pass in the style. The default Style refers to the default Style in the Theme used by the current Application or Activity.

Next, we will add content to our layout. For example, this layout is a RelativieLaoyut. We can pass
LayoutInflater.from(context).inflate(R.layout.imagebtn_with_text, this, true)
This line of code reads a layout directly from xml, but this will make the layout more nested, because the second parameter of the inflate function is the current layout, which is to add the layout read from the xml file to the in our custom layout. We can generate combined controls through the addView() method by creating our own controls.
    First read the custom properties, as follows:
//read properties collection
TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.GeneralTitle);
//Then take the custom attribute from the attribute set. If the corresponding attribute is not added in the xml, what is obtained here is null, and we can also use this value to determine whether to generate the corresponding control.
CharSequence title= ta.getText(R.styleable.GeneralTitle_title_text);
        int textColor = ta.getColor(R.styleable.GeneralTitle_title_textcolor,0xffffff);
        float textSize = ta.getDimension(R.styleable.GeneralTitle_title_textsize,18);
        Drawable leftPic = ta.getDrawable(R.styleable.GeneralTitle_title_left_pic);
        Drawable rightPic = ta.getDrawable(R.styleable.GeneralTitle_title_right_pic);
        float picWidth = ta.getDimension(R.styleable.GeneralTitle_title_pic_width,0);
        float title_leftpic_verticalpadding = ta.getDimension(R.styleable.GeneralTitle_title_leftpic_verticalpadding,0);
        float title_leftpic_horizontalpadding = ta.getDimension(R.styleable.GeneralTitle_title_leftpic_horizontalpadding,0);
        float title_rightpic_verticalpadding = ta.getDimension(R.styleable.GeneralTitle_title_rightpic_verticalpadding,0);
        float title_rightpic_horizontalpadding = ta.getDimension(R.styleable.GeneralTitle_title_rightpic_horizontalpadding,0);
        float title_leftpic_leftmagin = ta.getDimension(R.styleable.GeneralTitle_title_leftpic_leftmagin,0);
        float title_rightpic_rightmagin = ta.getDimension(R.styleable.GeneralTitle_title_rightpic_rightmagin,0);
ta.recycle();//Be sure to release the property after getting it.
        if(null != title) {
            //title
            TextView text_title = new TextView(context);
            text_title.setText(title);
            text_title.setTextColor(textColor);
            text_title.setTextSize(TypedValue.COMPLEX_UNIT_PX,textSize);
            LayoutParams ttp = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
            ttp.addRule(CENTER_IN_PARENT);
            this.addView(text_title, ttp);
        }
        if (null != leftPic){
            // image on the left
            ImageView imageview_left = new ImageView(context);
            imageview_left.setImageDrawable(leftPic);
            imageview_left.setPadding((int) title_leftpic_horizontalpadding, (int)title_leftpic_verticalpadding,(int)title_leftpic_horizontalpadding, (int)title_leftpic_verticalpadding);
            LayoutParams ilp = new LayoutParams((int) picWidth, ViewGroup.LayoutParams.MATCH_PARENT);
            ilp.addRule(ALIGN_PARENT_LEFT);
            ilp.addRule(CENTER_VERTICAL);
            ilp.setMargins((int) title_leftpic_leftmagin,0,0,0);
            imageview_left.setOnClickListener(this);
            imageview_left.setTag("investor_title_imageview_left");
            this.addView(imageview_left, ilp);
        }
        if(null != rightPic) {
            // image on the right
            ImageView Imageview_right = new ImageView(context);
            Imageview_right.setImageDrawable(rightPic);
            Imageview_right.setPadding((int)title_rightpic_horizontalpadding,(int)title_rightpic_verticalpadding,(int)title_rightpic_horizontalpadding,(int)title_rightpic_verticalpadding);
            LayoutParams irp = new LayoutParams((int)picWidth, ViewGroup.LayoutParams.MATCH_PARENT);
            irp.addRule(ALIGN_PARENT_RIGHT);
            irp.addRule (CENTER_VERTICAL);
            irp.setMargins(0,0, (int) title_rightpic_rightmagin,0);
            Imageview_right.setOnClickListener(this);
            Imageview_right.setTag("investor_title_imageview_right");
            this.addView(Imageview_right, irp);
        }

For specific cases, see: GeneralTitle --> http://git.oschina.net/renyuan_1991/MyWidget
to get attributes We can also use loop statements, which is written in the source code of TextView, as follows:
int resourceId = -1;
     TypedArray typeArray = context.obtainStyledAttributes(attrs, R.styleable.MyImageView);
     ImageView iv = new ImageView(context);
     TextView tv = new TextView(context);
     int N = typeArray.getIndexCount();
     for(int i = 0 ;i<N;i++){
          int attr = typeArray.getIndex(i);
          switch (attr) {
               case R.styleable.MyImageView_Oriental:
                   resourceId = typeArray.getInt (R.styleable.MyImageView_Oriental, 0);
                    //
                    this.setOrientation(resourceId == 1 ? LinearLayout.HORIZONTAL :                     LinearLayout.VERTICAL);
               break;
               case R.styleable.MyImageView_Text:
               //Get the resource, the second parameter is the default value
                   resourceId = typeArray.getResourceId(R.styleable.MyImageView_Text, 0);
                    //By judging whether the resource id exists, if it exists, it means that the resource id of the string is defined at the time of definition. Here, the character is obtained through the resource id. If it is not the resource id, it is directly to the string at the time of definition.
                   tv.setText(resourceId>0?                         typeArray.getResources().getText(resourceId):typeArray.getString(R.styleable.MyImageView_Text));
               break;
               case R.styleable.MyImageView_Src:
                   resourceId = typeArray.getResourceId(R.styleable.MyImageView_Src, 0);
                   //Get the resource id If there is one, set it to the image, if not, display the system icon directly
                   iv.setImageResource(resourceId>0?resourceId:R.drawable.ic_launcher);
               break;
         }
}
addView(iv);
addView (tv);
typeArray.recycle();//Finally, we need to clear the property collection

The properties are defined as follows:
<?xml version="1.0" encoding="utf-8"?>
<resources>

    <declare-styleable name="MyImageView">
        <attr name="Text" format="reference|string"></attr>
        <!-- There is no format for enumeration. You can only use these two attributes when you use it. You don't need to specify the name of the enumeration when you customize it. Get the name of this attr to get the attributes set by xml -->
        <attr name="Oriental">
            <enum name="Horizontal" value="1"></enum>
            <enum name="Vertical" value="0"></enum>
        </attr>
        <attr name="Src" format="reference|integer"></attr>
    </declare-styleable>

</resources>

This example does not involve the three necessary processes of view measurement, layout, and drawing. If you want to see how to use these methods to create a completely custom view, please see the previous blog: http://renyuan-1991 .iteye.com/blog/2232463
Please indicate the source when reprinting: http://renyuan-1991.iteye.com/blog/2306381
I hope that programming lovers can join this group, help each other and learn together. Group number: 141877583

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326694416&siteId=291194637