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。怎么办
如果这里btn的值为null的话,说明R.id.btnstart不在当前的ContentView里面配置文件里面。
new Thread()
{
@Override
public void run()
{
synchronized(this)
{
try
{
wait(1000); //1秒
}
catch (InterruptedException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//在这里面调用measureHeight方法
}
}.start();
public final class R {
public static final class attr {
}
public static final class drawable {
public static final int ic_launcher=0x7f020000;
}
public static final class id {
public static final int btndrop=0x7f060005;
public static final int btnleft=0x7f060006;
public static final int btnright=0x7f060007;
public static final int btnstart=0x7f060003;
public static final int btnturn=0x7f060004;
加线程的话语法错误 说不允许应用外部类的非终态变量protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
// TODO Auto-generated method stub
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
setMeasuredDimension(measureWidth(widthMeasureSpec), measureHeight(heightMeasureSpec));
}
btn.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
result = dm.heightPixels-btn.getMeasuredHeight();很简单啊,为什么就没人知道呢,还是得多看看api啊