自定义属性
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
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:wm="http://schemas.android.com/apk/res-auto"
tools:context=".MainActivity">
<com.skyrayinstrument.wumeng.autoattribute.MyAttribute
android:layout_width="match_parent"
android:layout_height="match_parent"
wm:my_age="25"
wm:my_name="WuMeng"
wm:my_bg="@drawable/girl"/>
</android.support.constraint.ConstraintLayout>
上面是布局文件
- 定义了三个自定义属性
- 注意命名空间的问题
<?xml version="1.0" encoding="utf-8"?>
<resources>
<!--定义名字叫MyAttribute属性集合-->
<declare-styleable name="MyAttribute">
<!--定义一个名字叫my_age并且类型是integer的属性-->
<attr name="my_age" format="integer"/>
<!--定义一个名字叫my_name并且类型是string的属性-->
<attr name="my_name" format="string"/>
<!--定义一个名字叫my_bg并且类型是reference|color的属性-->
<attr name="my_bg" format="reference|color"/>
</declare-styleable>
</resources>
上面是values/attrs.xml文件的内容
- 文件的名字是attrs
package com.skyrayinstrument.wumeng.autoattribute;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
/**
* @author WuMeng
* @date 2019/1/21
* Describe: 自定义属性
*/
public class MyAttribute extends View {
private int myAge;
private String myName;
private Bitmap myBg;
public MyAttribute(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
// 获取XML文件中的属性,有三种方式
// 1. 用命名空间获取
String my_age = attrs.getAttributeValue("http://schemas.android.com/apk/res-auto","my_age");
String my_name = attrs.getAttributeValue("http://schemas.android.com/apk/res-auto","my_name");
String my_bg = attrs.getAttributeValue("http://schemas.android.com/apk/res-auto","my_bg");
Log.d("WM","my_age ==" + my_age + "my_name ==" + my_name + "my_bg == " + my_bg);
//2. 遍历属性集合
for (int i = 0; i < attrs.getAttributeCount();i++) {
Log.d("WM",""+attrs.getAttributeName(i)+"---"+attrs.getAttributeValue(i));
}
//3. 使用系统工具,获取属性
TypedArray typedArray = context.obtainStyledAttributes(attrs,R.styleable.MyAttribute);
for (int i = 0; i < typedArray.getIndexCount(); i++) {
int index = typedArray.getIndex(i);
switch (index) {
case R.styleable.MyAttribute_my_age:
myAge = typedArray.getInt(index,0);
break;
case R.styleable.MyAttribute_my_name:
myName = typedArray.getString(index);
break;
case R.styleable.MyAttribute_my_bg:
Drawable drawable = typedArray.getDrawable(index);
BitmapDrawable bitmapDrawable = (BitmapDrawable) drawable;
myBg = bitmapDrawable.getBitmap();
break;
default:
break;
}
}
// 记得回收
typedArray.recycle();
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
Paint paint = new Paint();
canvas.drawText("" + myName+": " + myAge,50,50,paint);
canvas.drawBitmap(myBg,100,100,paint);
}
}
上面是获取XML
布局文件中的自定义属性值的三种方法
网友评论