Android:为什么在自定义视图中覆盖onMeasure()后,视图的文本无法在RalativeLayout中显示?

时间:2011-12-31 03:24:21

标签: android

我创建了一个扩展View并覆盖其onMeasure()的自定义组件,此组件的内容是一些文本,然后我将其添加到RelativeLayout,但此文字可以' t显示,如果我评论被覆盖的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>

1 个答案:

答案 0 :(得分:4)

您绝对可以将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决定使布局引擎“动态且智能化”,以尽可能多地支持尽可能多的应用程序。这只需要开发人员让设备准确知道它需要什么。

注意:听起来您确实想要,但请仔细考虑您的选择以及您将支持的设备数量。 :)