當(dāng)前位置:首頁(yè) > IT技術(shù) > 移動(dòng)平臺(tái) > 正文

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

續(xù)上,我們已經(jīng)掌握了自定義控件的基礎(chǔ)知識(shí)(不清楚的自己去查看Android自定義控件(一)---基礎(chǔ)篇),下面,我們來(lái)簡(jiǎn)單的寫(xiě)一個(gè)自定義 mTextView 控件(提醒一下:控件名字和屬性名字最好不要和系統(tǒng)一樣,容易報(bào)沖突錯(cuò)誤),廢話不多說(shuō),上碼?。?!

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

Android自定義控件(二)---實(shí)戰(zhàn)篇_安卓

?第二步:在 attrs.xml 文件夾里自定義一些自己規(guī)劃好的屬性,這里我們就簡(jiǎn)單寫(xiě)幾個(gè)

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

?Android自定義控件(二)---實(shí)戰(zhàn)篇_android studio_02

第三步:在布局文件中應(yīng)用

<?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="關(guān)注我,不迷路,哈哈哈,打了一波廣告"
        app:mTextSize="16dp"
        app:mTextColor="#ff0000"/>

</LinearLayout>

Android自定義控件(二)---實(shí)戰(zhàn)篇_移動(dòng)開(kāi)發(fā)_03

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

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、設(shè)置自定義屬性變量
    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、獲取裝有自定義屬性值的數(shù)值
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.mTextView);
        //3、精確獲取自定義屬性值
        mTextSize = typedArray.getDimensionPixelSize(R.styleable.mTextView_mTextSize,mTextSize);//源碼用的這個(gè)方法,參照 TextView
        mTextColor = typedArray.getColor(R.styleable.mTextView_mTextColor,mTextColor);
        mText = typedArray.getString(R.styleable.mTextView_mText);
        //4、回收 typedArray
        typedArray.recycle();
    }

    //5、測(cè)量
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

    //6、繪制
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
    }
}

寫(xiě)到這里,已經(jīng)成功了一大半,但是,如果你將項(xiàng)目運(yùn)行起來(lái),會(huì)發(fā)現(xiàn)什么也沒(méi)有,因?yàn)槟氵€沒(méi)有 測(cè)量(onMeasure)和繪制(onDraw),后續(xù)我們會(huì)詳細(xì)闡述這兩個(gè)方法。

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

開(kāi)通會(huì)員,享受整站包年服務(wù)立即開(kāi)通 >