Android 自定义属性时TypedArray的使用

来源:互联网 发布:企业社交网络平台 编辑:程序博客网 时间:2024/06/06 12:38

对于自定义属性,遵循以下几步,就可以实现:

  1. 自定义一个CustomView(extends View )类
  2. 编写res/values/attrs.xml,在其中编写styleable和item等标签元素
  3. 在布局文件中CustomView使用自定义的属性(注意namespace)
  4. 在CustomView的构造方法中通过TypedArray获取

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:app="http://schemas.android.com/apk/res-auto"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    android:background="@color/color_white"    android:gravity="center_horizontal"    android:orientation="vertical">              <com.demo.RoundCornerProgress                    android:id="@+id/progressBar"                    android:layout_width="300dp"                    android:layout_height="13dp"                    app:rcMax="100000"                    app:rcBackgroundColor="#dedede"                    app:rcProgressColor="#fcc329"                    app:rcRadius="6.5dp" /></LinearLayout>

com.demo.RoundCornerProgress是自定义的ProgressBar,布局文件中

app:rcMax="100000"app:rcBackgroundColor="#dedede"app:rcProgressColor="#fcc329"app:rcRadius="6.5dp"

是自定义的属性。

app是命名空间,自己可以随便命名其他名字,用来加在自定义属性前面。

xmlns:android=”http://schemas.android.com/apk/res/android

声明xml命名空间。xmlns意思为“xml namespace”.冒号后面是给这个引用起的别名。
schemas是xml文档的两种约束文件其中的一种,规定了xml中有哪些元素(标签)、元素有哪些属性及各元素的关系,当然从面向对象的角度理解schemas文件可以认为它是被约束的xml文档的“类”或称为“模板”。

早期或简单的xml用的是另一种约束,称为DTD,这东西大家天天都见到。html/xhtml中都存在(早期的html可能没有),如” html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN”
“http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd“。

现在大部分xml文档的约束都换成schema了,原因是schema本身也是xml,二schema扩展性强。

rcMax、rcProgress等就是xml里面自己命名的。

<?xml version="1.0" encoding="utf-8"?><resources xmlns:android="http://schemas.android.com/apk/res/android">    <!--进度条样式-->    <declare-styleable name="RoundCornerProgress">        <attr name="rcReverse" format="boolean"/>        <attr name="rcProgress" format="float"/>        <attr name="rcMax" format="float"/>        <attr name="rcSecondaryProgress" format="float"/>        <attr name="rcBackgroundPadding" format="dimension"/>        <attr name="rcRadius" format="dimension"/>        <attr name="rcProgressColor" format="color"/>        <attr name="rcSecondaryProgressColor" format="color"/>        <attr name="rcBackgroundColor" format="color"/>    </declare-styleable></resources>

其中的format的意义和可取的值有以下一些:

  • reference:表示引用,参考某一资源ID
    (1)属性定义:
    (2)属性使用:
<ImageViewandroid:layout_width = "42dip"android:layout_height = "42dip"android:background = "@drawable/图片ID"/>
  • color:颜色值
  • boolean:布尔值
  • dimension:尺寸值。注意,这里如果是dp那就会做像素转换
  • float:浮点值。
  • integer:整型值。
  • string:字符串
  • fraction:百分数。
  • enum:枚举值
  • flag:是自己定义的,类似于 android:gravity=”top”,就是里面对应了自己的属性值。
  • reference|color:颜色的资源文件。 12.reference|boolean:布尔值的资源文件

注意://由于reference是从资源文件中获取:所以在XML文件中写这个属性的时候必须personattr:name=”@string/app_name”这种格式,否则会出错


接着就可以在自定义控件中获取了

context通过调用obtainStyledAttributes方法来获取一个TypeArray,然后由该TypeArray来对属性进行设置
obtainStyledAttributes方法有三个,我们最常用的是有一个参数的obtainStyledAttributes(int[] attrs),其参数直接styleable中获得
TypedArray a = context.obtainStyledAttributes(attrs,R.styleable.MyView);
调用结束后务必调用recycle()方法,否则这次的设定会对下次的使用造成影响

public void setupStyleable(Context context, AttributeSet attrs) {        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.RoundCornerProgress);        radius = (int) typedArray                .getDimension(R.styleable.RoundCornerProgress_rcRadius, dp2px(DEFAULT_PROGRESS_RADIUS));        padding = (int) typedArray.getDimension(R.styleable.RoundCornerProgress_rcBackgroundPadding,                dp2px(DEFAULT_BACKGROUND_PADDING));        isReverse = typedArray.getBoolean(R.styleable.RoundCornerProgress_rcReverse, false);        max = typedArray.getFloat(R.styleable.RoundCornerProgress_rcMax, DEFAULT_MAX_PROGRESS);        progress = typedArray.getFloat(R.styleable.RoundCornerProgress_rcProgress, DEFAULT_PROGRESS);        secondaryProgress = typedArray.getFloat(R.styleable.RoundCornerProgress_rcSecondaryProgress,                DEFAULT_SECONDARY_PROGRESS);        int colorBackgroundDefault = context.getResources().getColor(                R.color.round_corner_progress_bar_background_default);        colorBackground = typedArray                .getColor(R.styleable.RoundCornerProgress_rcBackgroundColor, colorBackgroundDefault);        int colorProgressDefault = context.getResources().getColor(R.color.round_corner_progress_bar_progress_default);        colorProgress = typedArray.getColor(R.styleable.RoundCornerProgress_rcProgressColor, colorProgressDefault);        int colorSecondaryProgressDefault = context.getResources().getColor(                R.color.round_corner_progress_bar_secondary_progress_default);        colorSecondaryProgress = typedArray.getColor(R.styleable.RoundCornerProgress_rcSecondaryProgressColor,                colorSecondaryProgressDefault);        typedArray.recycle();        initStyleable(context, attrs);    }

在控件构造方法中调用该方法就能获取到值了

public RoundCornerProgressBar(Context context, AttributeSet attrs) {            setup(context, attrs);   }

AttributeSet中的确保存的是该View声明的所有的属性,并且可以通过它去获取(自定义的)属性。

public CustomView(Context context, AttributeSet attrs) {        super(context, attrs);        int count = attrs.getAttributeCount();        for (int i = 0; i < count; i++) {            String attrName = attrs.getAttributeName(i);            String attrVal = attrs.getAttributeValue(i);            Log.e(TAG, "attrName = " + attrName + " , attrVal = " + attrVal);        }    }

就能打印出所有属性。

attrName = layout_width , attrVal = 300.0dip
attrName = layout_height , attrVal = 13.0dip
attrName = app:rcMax , attrVal = 100000.0

1 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 宝宝吃鱼肝油吐怎么办 五个月婴儿夏天怎么办 厌奶期宝宝瘦了怎么办 二个月的宝宝不喝夜奶怎么办 婴儿不喝奶粉怎么办 小孩整天不吃饭怎么办 婴儿不吃不喝怎么办 断奶后不吃奶瓶怎么办 小孩早上不吃饭怎么办 新生儿不认乳头怎么办 宝宝不吸奶嘴怎么办 孩子不会吸奶瓶怎么办 宝宝突然不吃奶瓶怎么办 换了奶瓶不喝奶怎么办 新生儿不喝奶粉怎么办 7个月小婴儿磨牙怎么办 宝宝出生四天不喝母乳怎么办 我的奶水不足怎么办 乳牙长得不整齐怎么办 新生儿只吃奶粉怎么办 小孩不肯吸母乳怎么办 三个月宝宝不吃奶粉怎么办 宝宝不爱喝水怎么办 崔玉涛 小孩身体铅过高怎么办 疫苗引起的发烧怎么办 婴儿不吃米糊怎么办 宝宝米糊不吃怎么办 换奶瓶宝宝不吃怎么办 小孩不会吃奶瓶怎么办 百天不吃奶瓶怎么办 1岁宝宝积食怎么办 宝宝退烧后流汗怎么办 宝宝高烧后出汗怎么办 发烧出汗不退烧怎么办 婴儿发烧不出汗怎么办 婴幼儿发烧不退怎么办 宝宝突然不吃饭怎么办 宝宝吃饭到处跑怎么办 宝宝不吃奶瓶怎么办崔玉涛 八个月母乳不足怎么办 八个月宝宝厌食怎么办