如何获取自定义属性
来源:互联网 发布:淘宝网完美芦荟胶 编辑:程序博客网 时间: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
- 如何获取自定义属性
- js 如何获取自定义属性的值
- 自定义控件如何获取 android:layout_width属性
- 如何获取Input标签自定义属性的值
- Jquery如何获取select选中项 自定义属性的值?
- Jquery如何获取select选中项 自定义属性的值?
- Jquery如何获取select选中项 自定义属性的值?
- Jquery如何获取select选中项 自定义属性的值
- vuejs2.0如何获取dom元素自定义属性值
- 自定义获取属性文件
- Js获取自定义属性
- 自定义属性获取
- 获取自定义属性值
- option自定义属性获取
- 自定义View-获取自定义属性
- 如何自定义属性
- 自定义组件 获取属性值
- jquery获取option自定义属性
- JAVA入门基础详解之一
- nginx安装
- redis 数据结构
- 2016-2017 CT S03E05: Codeforces Trainings Season 3 Episode 5 J
- 单片机软定时器
- 如何获取自定义属性
- parse generator生成c++文件
- JavaScript入门<3>JSON
- java函数定义泛型
- Java String,StringBuilder,StringBuffer区别和实际应用总结
- ant control statements
- PyQt学习笔记(4)--Smart Dialog
- 视音频基础知识——YUV420
- 各种C++关键字的作用