`
mmdev
  • 浏览: 12954458 次
  • 性别: Icon_minigender_1
  • 来自: 大连
文章分类
社区版块
存档分类
最新评论

Android自定义属性时TypedArray的使用方法

 
阅读更多

有时候android传统的页面布局不足以满足我们的需求,常常需要自己定义view,通常继承View,然后重写构造方法以及onDraw等函数,再具体实现自己定义的复杂view。我们知道在给控件赋属性时,通常使用的是android系统自带的属性,比如 android:layout_height="wrap_content",除此之外,我们亦可以自己定义属性,这样在使用的时候我们就可以使用形如 myapp:myTextSize="20sp"的方式了,步骤大致如下:

1》在项目文件res/value下面创建一个attr.xml文件,该文件中包含若干个attr集合,例如:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="MyView">
        <attr name="myTextSize" format="dimension"/>
        <attr name="myColor" format="color"/>
    </declare-styleable>
</resources>


其中resource是跟标签,可以在里面定义若干个declare-styleable,<declare-styleable name="MyView">中name定义了变量的名称,下面可以再自定义多个属性,针对<attr name="myTextSize" format="dimension"/>来说,其属性的名称为"myTextSize",format指定了该属性类型为dimension,只能表示字体的大小。

format还可以指定其他的类型比如;

reference 表示引用,参考某一资源ID
string 表示字符串
color 表示颜色值
dimension 表示尺寸值
boolean 表示布尔值
integer 表示整型值
float 表示浮点值
fraction 表示百分数
enum 表示枚举值
flag 表示位运算

2》在使用到该自定义view的布局文件中键入如下的一行:

xmlns:myapp=http://schemas.android.com/apk/res/com.eyu.attrtextdemo绿色是自己定义属性的前缀名字,粉色是项目的包名,这样一来,在我们自己定义的view的属性中,就可以使用自己在attr中定义的属性啦,例如:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:myapp="http://schemas.android.com/apk/res/com.eyu.attrtextdemo"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />
    <com.eyu.attrtextdemo.MyView
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        myapp:myTextSize="20sp"
        myapp:myColor="#324243"/>

</LinearLayout>


3》在自定义view的代码中引入自定义属性,修改构造函数

context通过调用obtainStyledAttributes方法来获取一个TypeArray,然后由该TypeArray来对属性进行设置

obtainStyledAttributes方法有三个,我们最常用的是有一个参数的obtainStyledAttributes(int[] attrs),其参数直接styleable中获得

TypedArray a = context.obtainStyledAttributes(attrs,R.styleable.MyView);

调用结束后务必调用recycle()方法,否则这次的设定会对下次的使用造成影响

具体如下:

package com.eyu.attrtextdemo;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.util.AttributeSet;
import android.view.View;

public class MyView extends View{
	public Paint paint;

	public MyView(Context context, AttributeSet attrs) {
		super(context, attrs);
		paint = new Paint();
		
		TypedArray a = context.obtainStyledAttributes(attrs,R.styleable.MyView);	
		int textColor = a.getColor(R.styleable.MyView_myColor, 003344);
		float textSize = a.getDimension(R.styleable.MyView_myTextSize, 33);
		paint.setTextSize(textSize);
		paint.setColor(textColor);
		a.recycle();
	}

	public MyView(Context context) {
		super(context);
		// TODO Auto-generated constructor stub
	}
	
	@Override
	protected void onDraw(Canvas canvas) {
		// TODO Auto-generated method stub
		super.onDraw(canvas);	
		paint.setStyle(Style.FILL);
		canvas.drawText("aaaaaaa", 10, 50, paint);
	}
	
	
	
}

运行后:

分享到:
评论

相关推荐

    详解Android自定义控件属性TypedArray以及attrs

    主要为大家介绍了android自定义控件属性TypedArray以及attrs,感兴趣的小伙伴们可以参考一下

    Android中自定义属性attrs.xml、TypedArray的使用

    Android中自定义属性attrs.xml、TypedArray的使用,只是做的一个示例,不喜勿喷

    Android 自定义View时使用TypedArray配置样式属性详细介绍

    Android 自定义View时使用TypedArray配置样式属性详细介绍  在自定义view时为了提高复用性和扩展性,可以为自定义的view添加样式属性的配置,比如自定义图片资源、文字大小、控件属性等,就这需要用到TypedArray类...

    Aj_03的Android 中自定义属性(attr.xml,TypedArray)的使用(源码)

    测试:Android 中自定义属性(attr.xml,TypedArray)的使用 注意:MyView(Context context,AttributeSet attrs)构造函数的实现, 和注意main.xml的LinearLayout 里加的声明 要了解:test:textSize="20px" test:...

    Android 自定义view时用到的TypedArray

    在自定义控件的时候,如果我们想额外的添加一些属性,就会用到TypedArray这个类,那么这个类是怎么得到的,以及怎么使用的,这里作个简单介绍。 创建自定义属性 首先创建values\attrs.xml,在attrs.xml中声明自定义...

    Android自定义菜单属性DEMO

    http://blog.csdn.net/janice0529/article/details/34425549 博客日志源码

    Android 中自定义属性(attr.xml,TypedArray)的使用

    NULL 博文链接:https://elingwange.iteye.com/blog/1285289

    Android自定义图片集合

    本文主要包括以下内容: 使用Xfermode设置圆角图片 使用BitmapShader设置圆角图片 滑动旋转缩放的bimp图片 ...详解Android自定义控件属性TypedArray以及attrs 1、使用Xfermode设置圆角图片 主要代码

    Android自定义控件深入学习 Android生成随机验证码

    在上一篇的文章中介绍了自定义控件的属性,详情见《详解Android自定义控件属性TypedArray以及attrs》。那么在这基础上实现随机验证码生成,里面的代码是自定义控件以及涉及到自定义view绘画。 1、先看实现的效果图 ...

    Android如何自定义视图属性

    本文实例为大家介绍了Android自定义视图属性的方法,供大家参考,具体内容如下 1. 自定义一个自己的视图类继承自View public class MyView extends View { public MyView(Context context, AttributeSet attrs) {...

    Android自定义等待对话框

    最近,看了好多的APP的等待对话框,发现自己的太lower,于是就研究了一番,最后经过苦心努力,实现一个。 自定义一个LoadingIndicatorView(extends View )类 ...1、自定义属性的声明文件 &lt;declare-styleable n

    理解Android中的自定义属性

    本文实例讲解了Android中的自定义属性,具体内容如下 1、引言 对于自定义属性,大家肯定都不陌生,遵循以下几步,就可以实现: 自定义一个CustomView(extends View )类 编写values/attrs.xml,在其中编写styleable...

    Android高手进阶教程与Android基础教程

    Android高手进阶教程之----Android 中自定义属性(attr.xml,TypedArray)的使用! .doc Android高手进阶教程之----Android中万能的BaseAdapter(Spinner,ListView,GridView)的使用!.doc Android高手进阶教程之----通过...

    Android自定义圆角ImageView控件

    目前一些比较火的图片加载库虽然支持圆角加载,若你是...import android.content.res.TypedArray; import android.graphics.Bitmap; import android.graphics.Bitmap.Config; import android.graphics.BitmapFactory; im

    android实现图片验证码方法解析(自绘控件)

     在view的构造方法中 有一个AttributeSet的参数 很明显是用来保存控件属性信息的 我们也的确可以通过循环然后用键值对的方式获取信息 而TypedArray是用来简化我们的工作的  2.重写onMeasure 测量控件大小  3.重写...

    Android自定义圆环计时器——史上最详细篇

    自定义的圆环计时器提供了以下属性 代码与注释 package com.example.view; import android.animation.ValueAnimator; import android.content.Context; import android.content.res.TypedArray; ...

    理发师:一个用于Android的自定义视图样式库,可为您生成gainStyledAttributes()和TypedArray样板代码

    @AndroidAttr用于检索android名称空间中的属性值。 Barber类具有3个重载的style()方法,因此您可以从喜欢的任何构造函数中调用适当的方法。 带注释的字段或方法不能是私有的,并且至少必须是程序包可访问的。 这是...

    安卓自定义布局,SelectedItem

    纯Java实现自定义的布局,用到了...安卓自定义View,纯JAVA实现View的属性,用到了TypedArray。 ysf\libs\ItemView.java ysf\libs\SelectItemView.java ysf\libs\SwitchItemView.java ysf\libs\TextButton.java

Global site tag (gtag.js) - Google Analytics