如何测量iphone6控件尺寸寸

27266人阅读
Android(30)
上次讲的自定义控件刷新点屏幕的任意地方都会刷新,而且在xml里自定义控件下面放一个textview的话,这个TextView是显示不出来的,不只这个,以前的几个自定义控件都是
为什么呢?今天来讲下onMeasure()
在自定义刷新控件的基础上重写onMeasure方法
根据上一篇自定义组件修改
注释在代码里
&LinearLayout xmlns:android=&/apk/res/android&
android:orientation=&vertical&
android:layout_width=&fill_parent&
android:layout_height=&wrap_content&
&xue.test.CusView3
android:id=&@+id/cusview3&
android:layout_width=&wrap_content&
android:layout_height=&wrap_content&
&/xue.test.CusView3&
android:layout_width=&wrap_content&
android:layout_height=&wrap_content&
android:text=&我终于出现了& /&
&/LinearLayout&
这里的TextView无法显示,想要显示的话,要测量控件的大小
public class CusView3 extends View {
private int color = 0;
private String text = &点击我刷新&;
private Paint mP
private int mA
public CusView3(Context context, AttributeSet attrs) {
super(context, attrs);
mPaint = new Paint();
mPaint.setStyle(Style.FILL);
mPaint.setTextSize(35.0f);
setPadding(20, 60, 0, 0); //设置padding
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
if (color & 2) {
color = 0;
switch (color) {
mPaint.setColor(Color.GREEN);
mPaint.setColor(Color.RED);
mPaint.setColor(Color.BLUE);
canvas.drawText(text, getPaddingLeft(), getPaddingTop(), mPaint);
public void changeColor() {
* 比onDraw先执行
* 一个MeasureSpec封装了父布局传递给子布局的布局要求,每个MeasureSpec代表了一组宽度和高度的要求。
* 一个MeasureSpec由大小和模式组成
* 它有三种模式:UNSPECIFIED(未指定),父元素部队自元素施加任何束缚,子元素可以得到任意想要的大小;
EXACTLY(完全),父元素决定自元素的确切大小,子元素将被限定在给定的边界里而忽略它本身大小;
AT_MOST(至多),子元素至多达到指定大小的值。
*   它常用的三个函数:   
* 1.static int getMode(int measureSpec):根据提供的测量值(格式)提取模式(上述三个模式之一)
* 2.static int getSize(int measureSpec):根据提供的测量值(格式)提取大小值(这个大小也就是我们通常所说的大小)
* 3.static int makeMeasureSpec(int size,int mode):根据提供的大小值和模式创建一个测量值(格式)
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
setMeasuredDimension(measureWidth(widthMeasureSpec), measureHeight(heightMeasureSpec));
private int measureWidth(int measureSpec) {
int result = 0;
int specMode = MeasureSpec.getMode(measureSpec);
int specSize = MeasureSpec.getSize(measureSpec);
if (specMode == MeasureSpec.EXACTLY) {
// We were told how big to be
result = specS
// Measure the text
result = (int) mPaint.measureText(text) + getPaddingLeft() + getPaddingRight();
if (specMode == MeasureSpec.AT_MOST) {
// Respect AT_MOST value if that was what is called for by
// measureSpec
result = Math.min(result, specSize);// 60,480
private int measureHeight(int measureSpec) {
int result = 0;
int specMode = MeasureSpec.getMode(measureSpec);
int specSize = MeasureSpec.getSize(measureSpec);
mAscent = (int) mPaint.ascent();
if (specMode == MeasureSpec.EXACTLY) {
// We were told how big to be
result = specS
// Measure the text (beware: ascent is a negative number)
result = (int) (-mAscent + mPaint.descent()) + getPaddingTop() + getPaddingBottom();
if (specMode == MeasureSpec.AT_MOST) {
// Respect AT_MOST value if that was what is called for by
// measureSpec
result = Math.min(result, specSize);
&&相关文章推荐
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:597649次
积分:4207
积分:4207
排名:第7640名
原创:45篇
评论:150条
文章:11篇
阅读:203326
(1)(4)(1)(1)(3)(1)(2)(2)(1)(1)(1)(2)(1)(3)(3)(5)(5)(1)(9)(1)(4)
(window.slotbydup = window.slotbydup || []).push({
id: '4740881',
container: s,
size: '200,200',
display: 'inlay-fix'在开发 WinForm 控件时,通常需要测量文本绘出时的实际尺寸。 - LEE - 博客园
随笔 - 33, 文章 - 2, 评论 - 2, 引用 - 1
阅读要求:了解 C# 调用 Win32API 的基本原理和操作方式
在开发 WinForm 控件时,通常需要测量文本绘出时的实际尺寸。.NET FCL 中的 GDI+&类——System.Drawing.Graphics 提供了用于上述需要的 MeasureString 方法,该方法返回了一个 SizeF 结构的浮点数表示的结果,从表面上看起来似乎很精确,但在实际使用中发现有时此方法并不能精确测量出文本的实际宽度。也曾反编译 System.Drawing.Dll,但没看出什么名堂来。如果使用& GDI 却能很好地测量出文本的实际绘出宽度,下面提供了调用 GDI Win32API 来测量的 C# 实现源代码,以供参考。注: 代码中的 WindowsAPI 是一个自定义类,包装了大部分 Win32API 调用的 C# 定义。/// &summary&/// 在指定的矩形区域内,按照指定的格式,测量文本的实际绘出尺寸。/// &/summary&/// &param name="graphics"&绘图对象&/param&/// &param name="text"&被测量的文本&/param&/// &param name="font"&测量所用字体&/param&/// &param name="rc"&以矩形表示的要绘制区域&/param&/// &param name="drawFlags"&文本格式&/param&/// &returns&尺寸&/returns&public static Size GetTextSize(Graphics graphics, string text, Font font, Rectangle rc, DrawTextFormatFlags drawFlags){&// 一个记录设备上下文句柄的变量&IntPtr hdc = IntPtr.Z&&if ( graphics != null )&{&&// 获取与提供的 Graphics 关联的设备上下文句柄&&hdc = graphics.GetHdc();&}&else&{&&// 如果未提供 Graphics,使用屏幕作为设备上下文&&hdc = WindowsAPI.GetDC(IntPtr.Zero);&}
&// 测量所用字体的句柄&IntPtr fontHandle = font.ToHfont();
&// 将测量所用字体添加到设备上下文&// 并记录原来所使用的字体&IntPtr oldHfont = WindowsAPI.SelectObject(hdc, fontHandle);&
&// RECT 用于 Win32API 调用,.NET FCL 中的 Retangle 不适用于 Win32API 调用。&// 其定义如下:&//&// [StructLayout(LayoutKind.Sequential)]&// 这是必须的。&// public struct RECT&// {&//&&&&&//&&&&&//&&&&&//&&&&&// }
&// 创建一个 GDI Win32API 调用所需的 RECT 实例&RECT rect = new RECT();&rect.left = rc.L&rect.right = rc.R&rect.top = rc.T&rect.bottom = rc.B
// 文本绘制格式标志的枚举定义://&&& public enum DrawTextFormatFlags//&&& {//&&&&&&& DT_TOP&&&&&&&&&&&&& &&&&= 0x, //&&&&&&& DT_LEFT&&&&&&&&&&&& &&&&= 0x, //&&&&&&& DT_CENTER&&&&&&&&&& = 0x, //&&&&&&& DT_RIGHT&&&&&&&&&&& = 0x, //&&&&&&& DT_VCENTER&&&&&&&&& = 0x, //&&&&&&& DT_BOTTOM&&&&&&&&&& = 0x, //&&&&&&& DT_WORDBREAK&&&&&&& = 0x, //&&&&&&& DT_SINGLELINE&&&&&& = 0x, //&&&&&&& DT_EXPANDTABS&&&&&& = 0x, //&&&&&&& DT_TABSTOP&&&&&&&&& = 0x, //&&&&&&& DT_NOCLIP&&&&&&&&&& = 0x, //&&&&&&& DT_EXTERNALLEADING& = 0x, //&&&&&&& DT_CALCRECT&&&&&&&& = 0x, //&&&&&&& DT_NOPREFIX&&&&&&&& = 0x, //&&&&&&& DT_INTERNAL&&&&&&&& = 0x, //&&&&&&& DT_EDITCONTROL&&&&& = 0x, //&&&&&&& DT_PATH_ELLIPSIS&&& = 0x, //&&&&&&& DT_END_ELLIPSIS&&&& = 0x, //&&&&&&& DT_MODIFYSTRING&&&& = 0x, //&&&&&&& DT_RTLREADING&&&&&& = 0x, //&&&&&&& DT_WORD_ELLIPSIS&&& = 0x//&&& }
&// 调用 GDI Win32API 以测量文本的实际绘出时尺寸。&WindowsAPI.DrawText(hdc, text, text.Length, ref rect, &&(int)(drawFlags | DrawTextFormatFlags.DT_CALCRECT));
&// 重设为原来的字体,这是 GDI 必须的。&WindowsAPI.SelectObject(hdc, oldHfont);
&// 删除创建的测量用字体的句柄&WindowsAPI.DeleteObject(fontHandle);
&// 释放已获取的设备上下文句柄 &if ( graphics != null )&&graphics.ReleaseHdc(hdc);&else&&WindowsAPI.ReleaseDC(IntPtr.Zero, hdc);&&&&&&&&&// 返回实测结果&&&return new Size(rect.right - rect.left, rect.bottom - rect.top);}
以下是三个有用的重载方法:
public static Size GetTextSize(string text, Font font, Rectangle rc, DrawTextFormatFlags drawFlags){&return GetTextSize(null, text, font, rc, drawFlags);}
public static Size GetTextSize(Graphics graphics, string text, Font font){&// 设置一个文本绘制格式,以单行左对齐方式来测量。&DrawTextFormatFlags drawFlags =&&DrawTextFormatFlags.DT_SINGLELINE | &&DrawTextFormatFlags.DT_LEFT | &&DrawTextFormatFlags.DT_CALCRECT; // 这个标志表示要测量
&Rectangle rect = Rectangle.E
&return GetTextSize(graphics, text, font, rect, drawFlags) ;}
public static Size GetTextSize(string text, Font font){&return GetTextSize(null, text, font);}}

我要回帖

更多关于 vb.net 调整控件尺寸 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信