【自定义View】
来源:互联网 发布:曦力mac破解版 编辑:程序博客网 时间:2024/06/07 23:53
1.在res/attrs文件中自定义属性,format的取值范围有:
string、color、dimension、enum、reference、boolean、integer、float、fraction、flag
2.在自定义控件中声明定义的属性:
string是String类型,color、dimension、integer是int类型,float是float类型,boolean是boolean类型
2.在构造函数中获取自定义样式
使用TypedArray获取自定义属性
TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.VolumnControlBar, defStyleAttr, 0); int n = ta.getIndexCount(); for (int i = 0; i < n; i++) { int attr = ta.getIndex(i); switch (attr) { case R.styleable.VolumnControlBar_firstColor: mFirstColor = ta.getColor(attr, Color.GREEN); break; case R.styleable.VolumnControlBar_secondColor: mSecondColor = ta.getColor(attr, Color.CYAN); break; case R.styleable.VolumnControlBar_circleWidth: mCircleWidth = ta.getDimensionPixelSize(attr, (int) TypedValue.applyDimension( TypedValue.COMPLEX_UNIT_DIP, 20, getResources().getDisplayMetrics())); break; case R.styleable.VolumnControlBar_dotCount: mDotCount = ta.getInt(attr, 0); break; case R.styleable.VolumnControlBar_splitSize: mSplitSize = ta.getInt(attr, 0); break; case R.styleable.VolumnControlBar_image: mImage = BitmapFactory.decodeResource(getResources(), ta.getResourceId(attr, 0)); break; } } ta.recycle();
string格式getString
color格式getColor
int格式getInt
reference格式getResourceId
dimension格式getDimensionPixelSize
4.重写onMeasure方法
系统帮我们测量的高度和宽度都是MATCH_PARNET,当我们设置明确的宽度和高度时,系统帮我们测量的结果就是我们设置的结果,当我们设置为WRAP_CONTENT,或者MATCH_PARENT系统帮我们测量的结果就是MATCH_PARENT的长度。
调整字体大小适应范围
String msg = TextUtils.ellipsize(mTitle, textPaint, (float) mWidth - getPaddingLeft() - getPaddingRight(), TextUtils.TruncateAt.END).toString();canvas.drawText(msg, getPaddingLeft(), mHeight - getPaddingBottom(), mPaint);
5.重写onDraw方法
0 0
- 自定义view
- 自定义View
- 自定义view
- 自定义View
- 自定义View
- 自定义view
- 自定义View
- 自定义view
- 自定义view
- 自定义View
- 自定义View
- 自定义view
- 自定义view
- 自定义view
- 自定义view
- 自定义view
- 自定义View
- 自定义View
- 聚类分析
- HIHO 组合游戏,学习
- 利用bmob实现简单的注册和登录
- HDU1260简单DP
- Mac OS X 用终端破解附近 Wifi 密码教程
- 【自定义View】
- Unix信号说明
- kvm module之run vcpu
- 括号配对问题
- 生活中的数学 —— 几何
- selenium之 如何控制网页内嵌div中滚动条的滚动
- HDU 1712 ACboy needs your help
- Lightoj1282——Leading and Trailing(幂取模求前三位)
- HDU3336Count the string