计算控件尺寸
来源:互联网 发布:python 数据结构书籍 编辑:程序博客网 时间:2024/04/30 07:25
http://www.cnblogs.com/xirihanlin/archive/2009/07/23/1529238.html
除非你总是需要一个100×100像素的控件,否则,你必须要重写onMeasure。
onMeasure方法在控件的父元素正要放置它的子控件时调用。它会问一个问题,“你想要用多大地方啊?”,然后传入两个参数——
widthMeasureSpec和heightMeasureSpec。它们指明控件可获得的空间以及关于这个空间描述的元数据。
比返回一个结果要好的方法是你传递View的高度和宽度到setMeasuredDimension方法里。
接下来的代码片段给出了如何重写onMeasure。注意,调用的本地空方法是来计算高度和宽度的。它们会译解widthHeightSpec和heightMeasureSpec值,并计算出合适的高度和宽度值。
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int measuredHeight = measureHeight(heightMeasureSpec);
int measuredWidth = measureWidth(widthMeasureSpec);
setMeasuredDimension(measuredHeight, measuredWidth);
}
private int measureHeight(int measureSpec) {
// Return measured widget height.
}
private int measureWidth(int measureSpec) {
// Return measured widget width.
}
边界参数——widthMeasureSpec和heightMeasureSpec ,效率的原因以整数的方式传入。在它们使用之前,首先要做的是使用MeasureSpec类的静态方法getMode和getSize来译解,如下面的片段所示:
int specMode = MeasureSpec.getMode(measureSpec);
int specSize = MeasureSpec.getSize(measureSpec);
依据specMode的值,如果是AT_MOST,specSize 代表的是最大可获得的空间;如果是EXACTLY,specSize 代表的是精确的尺寸;如果是UNSPECIFIED,对于控件尺寸来说,没有任何参考意义。
当以EXACT方式标记测量尺寸,父元素会坚持在一个指定的精确尺寸区域放置View。在父元素问子元素要多大空间时,AT_MOST指示者会说给我最大的范围。在很多情况下,你得到的值都是相同的。
在两种情况下,你必须绝对的处理这些限制。在一些情况下,它可能会返回超出这些限制的尺寸,在这种情况下,你可以让父元素选择如何对待超出的View,使用裁剪还是滚动等技术。
接下来的框架代码给出了处理View测量的典型实现:
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int measuredHeight = measureHeight(heightMeasureSpec);
int measuredWidth = measureWidth(widthMeasureSpec);
setMeasuredDimension(measuredHeight, measuredWidth);
}
private int measureHeight(int measureSpec) {
int specMode = MeasureSpec.getMode(measureSpec);
int specSize = MeasureSpec.getSize(measureSpec);
// Default size if no limits are specified.
int result = 500;
if (specMode == MeasureSpec.AT_MOST)
{
// Calculate the ideal size of your
// control within this maximum size.
// If your control fills the available
// space return the outer bound.
result = specSize;
}
else if (specMode == MeasureSpec.EXACTLY)
{
// If your control can fit within these bounds return that value.
result = specSize;
}
return result;
}
private int measureWidth(int measureSpec) {
int specMode = MeasureSpec.getMode(measureSpec);
int specSize = MeasureSpec.getSize(measureSpec);
// Default size if no limits are specified.
int result = 500;
if (specMode == MeasureSpec.AT_MOST)
{
// Calculate the ideal size of your control
// within this maximum size.
// If your control fills the available space
// return the outer bound.
result = specSize;
}
else if (specMode == MeasureSpec.EXACTLY)
{
// If your control can fit within these bounds return that value.
result = specSize;
}
return result;
}
- 计算控件尺寸
- 计算控件尺寸
- 计算控件尺寸
- 计算控件尺寸
- Android 计算控件尺寸
- 【view】计算控件尺寸
- Android 计算控件尺寸
- Android 计算控件尺寸
- 文本显示尺寸计算(关于控件的尺寸定义)
- 计算尺寸
- 数码照片尺寸计算秘笈
- 计算字体大小及尺寸
- NSString 计算尺寸
- JS计算窗口尺寸
- iOS计算文本尺寸
- 计算文字尺寸
- 计算文字的尺寸
- UI窗口尺寸计算
- Axis开发Web Service程序
- 胡锦涛在深圳考察工作 勉励腾讯公司员工
- Understanding virtual memory and dynamic allocation
- Trie 树
- 飞利浦的十年转型 穆利堂-movno1 推荐
- 计算控件尺寸
- ARK---Kernel Star 学习windows内核
- Visual c++ Call Stack窗口调试 跟踪“内存不能为读”的错误
- 理解 typename关键字
- QT4.6.3 + VS2008安装配置全过程【转】
- 诺基亚的“一家人”策略 穆利堂-movno1推荐
- 同一表单中多个按钮的按下判断
- 左连接/右连接/全连接
- linux下的文件文件传输工具 sftp命令