android学习笔记16 - TypedArray自定义属性

来源:互联网 发布:nginx 多个虚拟主机 编辑:程序博客网 时间:2024/06/10 02:12

TypedArray用于自定义标签属性。

在目录res/value下面创建一个attr.xml文件,该文件中包含若干个attr集合,就是所有用户自定义TypedArray:

<?xml version="1.0" encoding="utf-8"?><resources>    <declare-styleable name="InputView">        <attr name="label" format="string" />        <attr name="special" format="boolean" />        <attr name="refrence" format="boolean" />        <attr name="background" format="string"></attr>        <attr name="editType" format="string"></attr>    </declare-styleable>    <declare-styleable name="TipView">        <attr name="fee" format="string" />        <attr name="type">            <enum name="input" value="0" />            <enum name="confirm" value="1" />            <enum name="card" value="2" />        </attr>    </declare-styleable>  

包含若干个declare-styleable标签:
<declare-styleable name="InputView">中name定义了变量的名称
<attr name="label" format="string" />标示属性名是label,format指定了该属性类型为string,只能表示字符串。
format还可以指定其他的类型比如:
reference   表示引用,参考某一资源ID
string   表示字符串
color   表示颜色值
dimension   表示尺寸值
boolean   表示布尔值
integer   表示整型值
float   表示浮点值
fraction   表示百分数
enum   表示枚举值
flag   表示位运算

调用方式如下:
首先需要定义一个视图,无论是自定义的view还是系统控件,以下以自定义的LinearLayout为例:
package com.example.parcelableserializable;import android.content.Context;import android.content.res.TypedArray;import android.graphics.Color;import android.util.AttributeSet;import android.util.TypedValue;import android.view.Gravity;import android.view.View;import android.view.View.OnClickListener;import android.view.View.OnFocusChangeListener;import android.widget.EditText;import android.widget.ImageView;import android.widget.LinearLayout;import android.widget.TextView;import android.widget.LinearLayout.LayoutParams;public class InputView extends LinearLayout implements OnFocusChangeListener,OnClickListener {private String label;private TextView labelTv;private LayoutParams labelLp; public InputView(Context context, AttributeSet attrs){super(context, attrs);TypedArray a = context.obtainStyledAttributes(attrs,R.styleable.InputView);label = (String) a.getText(R.styleable.InputView_label);a.recycle();// TODO Auto-generated constructor stublabelTv = new TextView(this.getContext());labelTv.setTextColor(Color.rgb(255, 0, 255));labelTv.setGravity(Gravity.CENTER_VERTICAL);labelTv.setTextSize(TypedValue.COMPLEX_UNIT_SP, 12);labelTv.setText(label);labelTv.setPadding(20, 0, 20, 0);setPadding(20, 0, 20, 0);setGravity(Gravity.CENTER_VERTICAL);setOrientation(LinearLayout.HORIZONTAL);setClickable(true);setOnClickListener(this);labelLp = new LayoutParams(android.view.ViewGroup.LayoutParams.WRAP_CONTENT,android.view.ViewGroup.LayoutParams.FILL_PARENT);this.addView(labelTv, labelLp);}@Overridepublic void onClick(View v){// TODO Auto-generated method stub}@Overridepublic void onFocusChange(View v, boolean hasFocus){// TODO Auto-generated method stub}}
修改构造函数context通过调用obtainStyledAttributes方法来获取一个TypeArray,obtainStyledAttributes(int[] attrs)其参数直接styleable中获得
TypedArray a = context.obtainStyledAttributes(attrs,R.styleable.InputView);
label = (String) a.getText(R.styleable.InputView_label);   
绑定该label,调用结束后务必调用recycle()方法。

使用这个自定义控件:
<com.example.parcelableserializable.InputView         android:id="@+id/input_view"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        app:label="测试"        />
这个app是未重写的标签名。效果如下:





0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 日本签证银行流水不够怎么办 想去日本cm签证怎么办 护照在签让那里怎么办 泰国出境单丢了怎么办 居住证到期了忘记续签了怎么办 贵州交警app忘记密码怎么办 科一第一次没过怎么办 社保转移时学历信息不符怎么办 天津摇号密码忘了怎么办 摇号账号密码忘了怎么办 京牌车去外地没有保险标怎么办 车子被扣12分怎么办 不在北京工作了社保怎么办 5年身份证到期了怎么办 c1驾照剩1分怎么办 c1驾校扣12分怎么办 我驾照扣了12分怎么办 小米手环绑定不了怎么办 小区总有小年青骑摩托车扰民怎么办 摩托车行驶证副本丢了怎么办 摩托车驾照副本丢了怎么办 公司行驶证掉了怎么办 身份证外迁了过户的话怎么办 驾照体检报告丢了怎么办 常州医保卡丢了怎么办 驾驶证违章罚单丢了怎么办 身份证被别人办了信用卡怎么办 被别人办了信用卡怎么办 考驾照体检忘带身份证怎么办 c证扣12分怎么办新规 c照12分不够扣怎么办 扣了18分怎么办一次性 c照累计扣12分怎么办 车辆超速扣12分怎么办 一次超速扣12分怎么办 分扣了罚款未交怎么办 c照一次扣12分怎么办 人在外地身份证到期了怎么办 手机进水了屏幕不亮怎么办 北京一证通过期怎么办 小米6音量键进水怎么办