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

2024-01-08

我制作了一个扩展的自定义组件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 布局渲染引擎将不知道要做什么,而只是隐藏该对象。必须明白的是,作为一个面向如此多设备的开放平台,谷歌决定让布局引擎变得“动态和智能”,以在尽可能多的平台上支持尽可能多的应用程序。这仅仅需要开发者让设备确切地知道它需要什么。

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

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Android:为什么在自定义视图中重写onMeasure()后,视图的文本无法在RalativeLayout中显示? 的相关文章

随机推荐