如何获取自定义属性

  • 在普通控件不能满足我们需求时,我们经常会用自定义控件为他设置我们需要的属性。今天为大家介绍获取自定义属性的三种方法

我想自定义一个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
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值