在Android開發中,大多數都是用Android提供的屬性,例如:
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textSize="40dp"
這些都是Android定義的,只是在使用Android定義的屬性,現在我們自己來自定義屬性
在自定義屬性之前,先去了解Android是如何自定義屬性的:需要找到SDK目錄中(D:\tools\sdk\platforms\android-28\data\res\values)
attrs.xml里面就是定義了Android的屬性規則:
name為屬性名稱,format為類型
自己自定義屬性:
myattribute:my_age="26"
myattribute:my_name="刀郎"
myattribute:my_bg="@mipmap/jtx"
注意:需要申請:xmlns:myattribute="http://schemas.android.com/apk/res-auto"
<!-- 自定義屬性 --> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" xmlns:myattribute="http://schemas.android.com/apk/res-auto" android:orientation="vertical" tools:context=".ShangGuiguTestActivity"> <view.custom.shangguigucustomview.MyCustomAttribute myattribute:my_age="26" myattribute:my_name="刀郎" myattribute:my_bg="@mipmap/jtx" android:layout_width="match_parent" android:layout_height="match_parent" /> </LinearLayout>
編寫 attrs.xml文件,來規則屬性類型:
<?xml version="1.0" encoding="utf-8"?> <resources> <declare-styleable name="MyCustomAttribute"> <attr name="my_age" format="integer" /> <attr name="my_name" format="string" /> <attr name="my_bg" format="reference" /> </declare-styleable> </resources>
使用自定義屬性:
public class MyCustomAttribute extends View { private static final String TAG = MyCustomAttribute.class.getSimpleName(); private Paint paint; public MyCustomAttribute(Context context, @Nullable AttributeSet attrs) { super(context, attrs); paint = new Paint(); paint.setAntiAlias(true); // 去鋸齒 paint.setTextSize(60); initView(context, attrs); } private int myage; private String myname; private Drawable mybg; private void initView(Context context, AttributeSet attributeSet) { // 1.通過命名控件來獲取 /*String age = attributeSet.getAttributeValue("http://schemas.android.com/apk/res-auto", "my_age"); String name = attributeSet.getAttributeValue("http://schemas.android.com/apk/res-auto", "my_name"); String bg = attributeSet.getAttributeValue("http://schemas.android.com/apk/res-auto", "my_bg"); Log.i(TAG, "age:" + age + " name:" + name + " bg:" + bg);*/ // 2.通過變量屬性方式打印獲取 /*for (int i=0; i<attributeSet.getAttributeCount(); i++) { Log.i(TAG, "name:" + attributeSet.getAttributeName(i) + " value:" + attributeSet.getAttributeValue(i)); }*/ // 3.通過控件方式來獲取,比較靠譜,這種方式才可以把圖片顯示 TypedArray typedArray = context.obtainStyledAttributes(attributeSet, R.styleable.MyCustomAttribute); myage = typedArray.getInt(R.styleable.MyCustomAttribute_my_age, 0); myname = typedArray.getString(R.styleable.MyCustomAttribute_my_name); mybg = typedArray.getDrawable(R.styleable.MyCustomAttribute_my_bg); typedArray.recycle(); // 因為源碼中是進行回收的 } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { // super.onMeasure(widthMeasureSpec, heightMeasureSpec); // 測量 寬度 高度 setMeasuredDimension(1200, 3000); } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); // 繪制我的年齡相關信息 canvas.drawText(String.valueOf(myage), 60, 100, paint); // 繪制我的名稱相關信息 canvas.drawText(myname, 60, 180, paint); // 繪制圖片 canvas.drawBitmap(((BitmapDrawable)mybg).getBitmap(), 60, 250, paint); } }
效果圖: