Android自定义属性,format
16lz
2021-12-19
Android自定义属性-format
string 字符串
- 定义
- 获取
TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.MyTextView); //获取文字 String textName = typedArray.getString(R.styleable.MyTextView_my_textname); this.setText(textName);
- 使用
dimension 尺寸值
- 定义
- 获取
TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.MyTextView); //获取文字 String textName = typedArray.getString(R.styleable.MyTextView_my_textname); this.setText(textName); //获取文字大小 float textsize = typedArray.getDimensionPixelSize(R.styleable.MyTextView_my_textsize, 14); this.setTextSize(textsize);
- 使用
color:颜色值
- 定义
- 获取
TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.MyTextView); //获取文字 String textName = typedArray.getString(R.styleable.MyTextView_my_textname); this.setText(textName); //获取文字大小 float textsize = typedArray.getDimensionPixelSize(R.styleable.MyTextView_my_textsize, 14); this.setTextSize(textsize); //获取文字颜色 int color = typedArray.getColor(R.styleable.MyTextView_my_textcolor, 0xff00ff00); this.setTextColor(color);
- 使用
reference:参考某一资源ID。
- 定义
- 获取
TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.MyTextView); //获取文字 String textName = typedArray.getString(R.styleable.MyTextView_my_textname); this.setText(textName); //获取文字大小 float textsize = typedArray.getDimensionPixelSize(R.styleable.MyTextView_my_textsize, 14); this.setTextSize(textsize); //获取文字颜色 int color = typedArray.getColor(R.styleable.MyTextView_my_textcolor, 0xff00ff00); this.setTextColor(color); //获取背景 int resourceId = typedArray.getResourceId(R.styleable.MyTextView_my_background, R.mipmap.ic_launcher); this.setBackgroundResource(resourceId);
- 使用
mytextview.png boolean:布尔值.
- 定义
- 获取
- 使用
integer:整型值
- 定义
- 获取
typedArray.getInteger(R.styleable.名称,默认值)
- 使用
float:浮点值
- 定义
- 获取
typedArray.getFloat(R.styleable.名称,默认值)
- 使用
fraction:百分数
- 定义
- 使用
enum:枚举值
- 定义
- 使用
flag:位或运算
- 定义
- 使用
属性定义时可以指定多种类型值
个人主页:
https://ln0491.github.io/
http://ln0491.coding.me/
博客:
http://blog.csdn.net/ko0491
http://www.cnblogs.com/liunanjava/
更多相关文章
- Android(安卓)Training - 使用IntentService执行任务(Lesson 3 -
- Android中一直走马灯效果的TextView
- 【Android】编译CM10遇到的错误解决方案
- android 获取 json 数据
- android 滑动动画效果
- 箭头函数的基础使用
- NPM 和webpack 的基础使用
- Python list sort方法的具体使用
- 【阿里云镜像】使用阿里巴巴DNS镜像源——DNS配置教程