我制作了一个自定义组件,该组件扩展了VIEw并覆盖了它的onMeasure(),该组件的内容是一些文本,然后将其添加到relativeLayout中,但是如果我评论了被覆盖的onMeasure(),则此文本将无法显示.文字显示.什么原因?
这是代码:
public class CustomVIEw extends VIEw { private String text; private int vIEwWIDth; private int vIEwHeight; private Paint paint; private FontMetrics FontMetrics; public CustomVIEw(Context context) { this(context, null); } public CustomVIEw(Context context, String text) { this(context, text, 0); this.text = text; paint = new Paint(Paint.ANTI_AliAS_FLAG); updateVIEwBounds(); }public CustomVIEw(Context context, String text, int defStyle) { super(context);}private voID updateVIEwBounds(){ vIEwWIDth = (int) paint.measureText(this.text); FontMetrics = paint.getFontMetrics(); vIEwHeight = (int)(FontMetrics.descent - FontMetrics.ascent); }private String getText() { return this.text;}protected voID onMeasure(int wIDthMeasureSpec, int heightmeasureSpec) { super.onMeasure(wIDthMeasureSpec, heightmeasureSpec); setMeasuredDimension(vIEwWIDth, vIEwHeight); //setMeasuredDimension(560, 100);even though give a ensured size, it can't //anyway.}protected voID onDraw(Canvas canvas) { super.onDraw(canvas); paint.setcolor(color.WHITE); paint.setTextSize(30); canvas.drawText(text, 0, 200, paint); Log.e("content", ""+this.getText());}public boolean ontouchEvent (MotionEvent event){ Log.e("touch", ""+this.getText()); return false;}}
这是活动:
public class CustomVIEwActivity extends Activity { /** Called when the activity is first created. */ private relativeLayout contentLayout; private CustomVIEw vIEw1; @OverrIDe public voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentVIEw(R.layout.main); contentLayout = (relativeLayout)findVIEwByID(R.ID.contentLayout); vIEw1 = new CustomVIEw(this, "You drive me crazy!!!"); relativeLayout.LayoutParams layoutParams = new relativeLayout.LayoutParams( LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); vIEw1.setLayoutParams(layoutParams); contentLayout.addVIEw(vIEw1); }}
这是XML文件:
<?xml version="1.0" enCoding="utf-8"?><relativeLayout xmlns:androID="http://schemas.androID.com/apk/res/androID" androID:ID="@+ID/contentLayout" androID:layout_wIDth="1024px" androID:layout_height="560px" androID:orIEntation="vertical" > <button androID:ID="@+ID/button2" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:layout_alignParentBottom="true" androID:layout_alignParentleft="true" androID:layout_marginleft="126dp" androID:text="button" /></relativeLayout>
解决方法:
您可以绝对将MeasureSpec设置为其他大小,但是onMeasure的参数具有误导性. MeasureSpec是经过特殊转换的int,必须同时使用像素度量和标志来专门创建.设置特定尺寸的正确方法如下所示…
final int desiredHSpec = MeasureSpec.makeMeasureSpec(pixelHeight, MeasureSpec.MODE_CONSTANT);final int desireDWSpec = MeasureSpec.makeMeasureSpec(pixelWIDth, MeasureSpec.MODE_CONSTANT);setMeasuredDimension(desireDWSpec, desiredHSpec);
MODE_CONSTANTS必须具有以下值之一:
* AT_MOST-表示它是动态的,但是如果内容太大,它将被裁剪
*准确-表示无论内容大小是多少
*未注明-表示它将根据父母,孩子,设备大小等参数做出任何决定.
如果您未指定这些常量之一,则AndroID Layout渲染引擎不知道该怎么做,而只是隐藏对象.必须理解,作为众多设备的开放平台,Google决定使布局引擎“动态且智能”,以在尽可能多的平台上支持尽可能多的应用程序.这仅要求开发人员让设备确切知道其需求.
注意:听起来确实很需要,但请仔细考虑您的选择以及支持的设备数量. 总结
以上是内存溢出为你收集整理的Android:为什么在自定义视图中覆盖onMeasure()之后,视图的文本无法在RalativeLayout中显示?全部内容,希望文章能够帮你解决Android:为什么在自定义视图中覆盖onMeasure()之后,视图的文本无法在RalativeLayout中显示?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)