android学习笔记16 - TypedArray自定义属性
2015-11-27 16:29
525 查看
TypedArray用于自定义标签属性。
在目录res/value下面创建一个attr.xml文件,该文件中包含若干个attr集合,就是所有用户自定义TypedArray:
包含若干个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为例:
修改构造函数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()方法。
使用这个自定义控件:
在目录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 stub labelTv = 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); } @Override public void onClick(View v) { // TODO Auto-generated method stub } @Override public 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是未重写的标签名。效果如下:
相关文章推荐
- android 优化Lint清除无效资源文件的用法
- Android中的AlertDialog实现
- Android-类qq功能(四)
- Android 自定义EditText( 登录界面)
- Android中对Log日志文件的分析
- android应用中去掉标题栏的方法
- Android性能测试之内存泄露以及GC机制浅析(三)
- --Android主题和样式之系统篇(下)
- Android-类qq功能(三)
- android 5.0 Launcher3,Gallery2中响应按键,实现翻页效果
- Android-类qq功能(二)
- Android主题和样式之系统篇(上)
- Android SQLite数据库操作——增(二)
- Android状态选择器
- android自定义弹出框样式实现
- Android学习——二维码生成 设置大小,白边 等
- Android ——SoundPool播放音频
- Android-类qq功能(一)
- android 启动失败和kernel 飞掉的log信息解读(MIPS)
- [转]Android 如何对sqlite数据库进行增删改[insert、update和delete] 操作