- Java code
private int measureHeight(int measureSpec) { // TODO Auto-generated method stub 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 = specSize; } else { // Measure the text DisplayMetrics dm = new DisplayMetrics(); if(this.getContext() instanceof Activity){ ((Activity) this.getContext()).getWindowManager().getDefaultDisplay().getMetrics(dm); } Button btn = (Button)findViewById(R.id.btnstart); result = dm.heightPixels-btn.getHeight(); if (specMode == MeasureSpec.AT_MOST) { // Respect AT_MOST value if that was what is called for by measureSpec result = Math.min(result, specSize); } } return result; }
我这么写得不到按钮的高啊,调试时btn的值是null。怎么办
------解决方案--------------------
把super.onMeasure放到setMeasuredDimension后面去看看