如何获取自定义属性

来源:互联网 发布:淘宝网完美芦荟胶 编辑:程序博客网 时间:2024/06/08 01:13
  • 在普通控件不能满足我们需求时,我们经常会用自定义控件为他设置我们需要的属性。今天为大家介绍获取自定义属性的三种方法

我想自定义一个view 绘制文本
1.首先我在values下新建attrs.xml,并设置文字属性zidiyitext,类型为String

<?xml version="1.0" encoding="utf-8"?><resources>    <declare-styleable name="MyText">        <attr name="zidiyitext" format="string" />    </declare-styleable></resources>

2.新建一个类继承View,并重写他的三个构造方法

public class MyText extends View 

3.获取自定义属性

       //第一种方法,直接通过遍历attrs获取        for (int i =0;i<attrs.getAttributeCount();i++){            System.out.println(attrs.getAttributeName(i)+"====="+attrs.getAttributeValue(i));        }        //第二种方法,通过名称空间获取        String age = attrs.getAttributeValue("http://schemas.android.com/apk/res-auto","zidiyitext");        //第三种方法,通过系统工具        TypedArray typedArray =  context.obtainStyledAttributes(attrs,R.styleable.MyText);        for(int i=0;i<typedArray.getIndexCount();i++){            int index =  typedArray.getIndex(i);            switch (index){                case R.styleable.MyText_zidiyitext:                    mytext = typedArray.getString(index);                    break;            }        }        typedArray.recycle();    }

4.在重写ondraw()在这里通过获取的属性 绘制文本

  @Override    protected void onDraw(Canvas canvas) {        super.onDraw(canvas);        Paint paint =new Paint();        canvas.drawText(mytext,100,100,paint);    }

5.在布局文件设置自定义属性

  <com.example.yinhaoyu.myapplication.MyText      android:layout_centerInParent="true"      android:layout_width="200dp"      android:layout_height="200dp"      app:zidiyitext="我是自定义值"/>

最后附上全部代码

package com.example.yinhaoyu.myapplication;import android.content.Context;import android.content.res.TypedArray;import android.graphics.Canvas;import android.graphics.Paint;import android.util.AttributeSet;import android.view.View;/** * Created by YinHaoYu on 2016/10/16. */public class MyText extends View {    private  String mytext;    public MyText(Context context) {        this(context,null);    }    public MyText(Context context, AttributeSet attrs) {        this(context, attrs,0);    }    public MyText(Context context, AttributeSet attrs, int defStyleAttr) {        super(context, attrs, defStyleAttr);        //第一种,直接通过遍历attrs获取        for (int i =0;i<attrs.getAttributeCount();i++){            System.out.println(attrs.getAttributeName(i)+"====="+attrs.getAttributeValue(i));        }        //第二种,通过名称空间获取        String age = attrs.getAttributeValue("http://schemas.android.com/apk/res-auto","zidiyitext");        //第三种,通过系统工具        TypedArray typedArray =  context.obtainStyledAttributes(attrs,R.styleable.MyText);        for(int i=0;i<typedArray.getIndexCount();i++){            int index =  typedArray.getIndex(i);            switch (index){                case R.styleable.MyText_zidiyitext:                    mytext = typedArray.getString(index);                    break;            }        }        typedArray.recycle();    }    @Override    protected void onDraw(Canvas canvas) {        super.onDraw(canvas);        Paint paint =new Paint();        canvas.drawText(mytext,100,100,paint);    }}
0 0
原创粉丝点击