• 當前位置:首頁 > IT技術 > 移動平臺 > 正文

    Android自定義控件(二)---實戰篇
    2021-09-16 11:53:56

    續上,我們已經掌握了自定義控件的基礎知識(不清楚的自己去查看Android自定義控件(一)---基礎篇),下面,我們來簡單的寫一個自定義 mTextView 控件(提醒一下:控件名字和屬性名字最好不要和系統一樣,容易報沖突錯誤),廢話不多說,上碼?。?!

    第一步:在 res 文件 下的 values 文件夾下新建一個名稱為 attrs 的 attrs.xml 文件(這里可以改成別的文件名,但我覺得沒必要,別閑的蛋疼)

    Android自定義控件(二)---實戰篇_安卓

    ?第二步:在 attrs.xml 文件夾里自定義一些自己規劃好的屬性,這里我們就簡單寫幾個

    <?xml version="1.0" encoding="utf-8"?>
    <resources>
        <!-- name 最好是自定義view的名字 例如:mTextView -->
        <declare-styleable name="mTextView">
            <!-- name 屬性名稱 不要和系統沖突
            format 格式  string 字符串
                         color 顏色
                         dimension 寬高 字體大小
                         integer 數字
                         reference 資源(drawable)
            -->
            <attr name="mTextSize" format="dimension"/>
            <attr name="mTextColor" format="color"/>
            <attr name="mText" format="string"/>
        </declare-styleable>
    </resources>

    ?Android自定義控件(二)---實戰篇_android studio_02

    第三步:在布局文件中應用

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        tools:context=".MainActivity">
    
        <com.example.mytextview.mTextView
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:background="@color/colorAccent"
            app:mText="關注我,不迷路,哈哈哈,打了一波廣告"
            app:mTextSize="16dp"
            app:mTextColor="#ff0000"/>
    
    </LinearLayout>

    Android自定義控件(二)---實戰篇_移動開發_03

    第四步:新建一個 自定義控件的類,如 mTextView,在這個類里獲取自定義屬性值

    package com.example.mytextview;
    
    //import javax.swing.text.View;
    import android.content.Context;
    import android.content.res.TypedArray;
    import android.graphics.Canvas;
    import android.graphics.Color;
    import android.util.AttributeSet;
    import android.view.View;
    
    import androidx.annotation.Nullable;
    
    public class mTextView extends View {
        //1、設置自定義屬性變量
        private int mTextSize = 16;
        private int mTextColor = Color.RED;
        private String mText; 
        
        public mTextView(Context context) {
            this(context,null);
        }
    
        public mTextView(Context context, @Nullable AttributeSet attrs) {
            this(context, attrs,0);
        }
    
        public mTextView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
            super(context, attrs, defStyleAttr);
    
            //2、獲取裝有自定義屬性值的數值
            TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.mTextView);
            //3、精確獲取自定義屬性值
            mTextSize = typedArray.getDimensionPixelSize(R.styleable.mTextView_mTextSize,mTextSize);//源碼用的這個方法,參照 TextView
            mTextColor = typedArray.getColor(R.styleable.mTextView_mTextColor,mTextColor);
            mText = typedArray.getString(R.styleable.mTextView_mText);
            //4、回收 typedArray
            typedArray.recycle();
        }
    
        //5、測量
        @Override
        protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
            super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        }
    
        //6、繪制
        @Override
        protected void onDraw(Canvas canvas) {
            super.onDraw(canvas);
        }
    }
    

    寫到這里,已經成功了一大半,但是,如果你將項目運行起來,會發現什么也沒有,因為你還沒有 測量(onMeasure)和繪制(onDraw),后續我們會詳細闡述這兩個方法。

    本文摘自 :https://blog.51cto.com/u

    開通會員,享受整站包年服務
    国产呦精品一区二区三区网站|久久www免费人咸|精品无码人妻一区二区|久99久热只有精品国产15|中文字幕亚洲无线码