Quick reading original book - Introduction to Android Application Development (customized view)

7.3 customized views

The meaning of user-defined View is to implement a class that extends android.view.View class through the extension method. The essence of this class is also a control, through which the UI can be built directly.

Refer to the example program: CustomView (apidemo = > views = > CustomView)
Source code: com/example/android/apis/view/CustomView1.java

com/example/android/apis/view/LabelView.java

Layout file: custom view 1.xml
The operation result of the CustomView program is shown as follows:

The layout file custom view 1.xml is as follows:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
 xmlns:app="http://schemas.android.com/apk/res/com.example.android.apis" 
 android:orientation="vertical" 
 android:layout_width="fill_parent" 
 android:layout_height="wrap_content"> 
 <com.example.android.apis.view.LabelView 
 android:background="@drawable/red" 
 android:layout_width="fill_parent" 
 android:layout_height="wrap_content" 
 app:text="Red"/> 
 <com.example.android.apis.view.LabelView 
 android:background="@drawable/blue" 
 android:layout_width="fill_parent" 
 android:layout_height="wrap_content" 
 app:text="Blue" app:textSize="20dp"/> 
 <com.example.android.apis.view.LabelView 
 android:background="@drawable/green" 
 android:layout_width="fill_parent" 
 android:layout_height="wrap_content" 
 app:text="Green" app:textColor="#ffffffff" /> 
</LinearLayout>

The label com.example.android.apis.view.LabelView used here is not a subclass of the View provided by the Android framework layer, but a class implemented in your own program. The classes that can be used in layout files are also the inheritors of android.view.View.

This com.example.android.apis.view.LabelView is implemented in the source file LabView.java. The main fragments are as follows:

    public class LabelView extends View {
        public LabelView(Context context, AttributeSet attrs) {
            super(context, attrs);
            initLabelView();
            TypedArray a = context.obtainStyledAttributes(attrs,
                    R.styleable.LabelView);
            CharSequence s = a.getString(R.styleable.LabelView_text);
            if (s != null) {
                setText(s.toString());
            }
            setTextColor(a.getColor(R.styleable.LabelView_textColor, 0xFF000000));
            int textSize =
                    a.getDimensionPixelOffset(R.styleable.LabelView_textSize, 0);
            if (textSize > 0) {
                setTextSize(textSize);
            }
            a.recycle();
        }
        protected void onDraw(Canvas canvas) {
            super.onDraw(canvas);
            canvas.drawText(mText, getPaddingLeft(),
                    getPaddingTop() - mAscent, mTextPaint);
        }
    }

In the constructor of LabelView, several attributes unique to LabelView are obtained through context.obtainStyledAttributes. R.styleable.LabelView is defined in attrs.xml file of res/values /. The contents are as follows:

 <declare-styleable name="LabelView"> 
	 <attr name="text" format="string" /> 
	 <attr name="textColor" format="color" /> 
	 <attr name="textSize" format="dimension" /> 
 </declare-styleable>

According to the class name implemented by LabView.java, the custom control can also be used in the layout file, and the use of the label is consistent with the class name. R. Styleable. LabelView? Text, r.styleable.LabelView? Textcolor and r.styleable.LabelView? Textsize are the attributes used in the source code, which correspond to several attributes such as app:text, app:textColor and app:textSize in the layout file that references LabelView. As public properties, LabelView should also have public functions to set these properties. These functions are as follows:

    public void setText(String text) {
        mText = text;
        requestLayout();
        invalidate();
    }
    public void setTextSize(int size) {
        mTextPaint.setTextSize(size);
        requestLayout();
        invalidate();
    }
    public void setTextColor(int color) {
        mTextPaint.setColor(color);
        invalidate();
    }

The above functions correspond to several attributes in XML. Without them, these attributes can only be specified in the XML file, not in the JAVA source file.

In the application layer of Android, you can extend android.view.View or its extender to realize your own View.

1031 original articles published, praised by 850 and visited by 30000+
His message board follow

Tags: Android Java xml

Posted on Mon, 13 Jan 2020 03:05:44 -0800 by robcrozier