自定义属性

首先在value里面创建一个attrs.xml:代码如下

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="AttributeView">
        <attr name="bg" format="reference|color" />
        <attr name="name" format="string" />
        <attr name="id" format="integer" />
    </declare-styleable>
</resources>

在布局文件中添加属性:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:ly="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.administrator.AutoAttribute.MainActivity">

    <com.example.administrator.AutoAttribute.AttributeView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        ly:bg="@drawable/meizi"
        ly:id="01"
        ly:name="自定义属性" />
</RelativeLayout>



package com.example.administrator.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.util.AttributeSet;
import android.view.View;

/**
 * Created by Administrator on 2017/5/16.
 */

public class AttributeView extends View {
    private static final String TAG = "TAG";
    private Bitmap bitmap;
    private int id;
    private String name;
    private Paint mPaint;
    private Paint textPaint;
    public AttributeView(Context context, AttributeSet attrs) {
        super(context, attrs);
        mPaint = new Paint();
        textPaint=new Paint();
        textPaint.setTextSize(40);
        mPaint.setAntiAlias(true);
        /**
         * 获取属性有三钟方法:
         */
        //方法一:
        /**
         * namespace:命名空间的值
         Namespace of attribute to retrieve.
         attribute:属性
         The attribute to retrieve.
         */
//        String name = attrs.getAttributeValue("http://schemas.android.com/apk/res-auto", "name");
//        String id = attrs.getAttributeValue("http://schemas.android.com/apk/res-auto", "id");
//        String bg = attrs.getAttributeValue("http://schemas.android.com/apk/res-auto", "bg");
//        Log.e(TAG, "name==" + name + ",id==" + id + ",bg==" + bg);
        //方法二:遍历属性集合
//        for (int i = 0; i < attrs.getAttributeCount(); i++) {
//            Log.e(TAG, attrs.getAttributeName(i)+"------"+attrs.getAttributeValue(i));
//        }
        //方法三:使用系统工具,获取属性(建议用法)
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.AttributeView);
        for (int i = 0; i < typedArray.getIndexCount(); i++) {
            int index = typedArray.getIndex(i);
            switch (index) {
                case R.styleable.AttributeView_name:
                    name = typedArray.getString(index);
                    break;
                case R.styleable.AttributeView_id:
                    id = typedArray.getInt(index, 0);
                    break;
                case R.styleable.AttributeView_bg:
                    Drawable b = typedArray.getDrawable(index);
                    BitmapDrawable bd = (BitmapDrawable) b;
                    bitmap = bd.getBitmap();
                    break;
            }
        }
	typedArray.recycle();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.drawText(name, getWidth()/2-name.length()/2, 50, textPaint);
        canvas.drawBitmap(bitmap, 0, 80, mPaint);
    }
}

获取属性值有三种方法:

方法一:Log:

方法二:log:




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值