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



我制作了一个扩展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 -意味着它是动态的,但如果内容太大,将被剪切* EXACTLY -意思是无论内容有多大或多小,它都会是那个大小*未指定-意味着它将根据父母,孩子,设备大小等参数做出任何决定…

如果你没有指定这些常量之一,那么Android布局渲染引擎不知道该怎么做,只是隐藏对象。必须理解的是,作为一个面向众多设备的开放平台,谷歌决定让布局引擎"动态和智能",以支持尽可能多的平台上的尽可能多的应用程序。这只需要开发人员让设备确切地知道它需要什么。

注意:听起来你想要EXACTLY,但是仔细考虑你的选择和你将支持多少设备。:)

最新更新