当使用填充器(inflate)后,如何获取控件的宽和高?

eg:

popView = LayoutInflater.from(this).inflate(R.layout.pop_bubble_1, null);
Log.i("[YT]", "popView-->W"+popView.getMeasuredWidth());
Log.i("[YT]", "popView-->H"+popView.getMeasuredHeight());
popView.measure(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
Log.i("[YT]", "popView-->W"+popView.getWidth());
Log.i("[YT]", "popView-->H"+popView.getHeight())
Log.i("[YT]", "popView-->W"+popView.getMeasuredWidth());
Log.i("[YT]", "popView-->H"+popView.getMeasuredHeight());


从上输出可以看出:只有最后两行才是真的popView的宽和高,其他四行输出全是o


总结:想要获取填充布局文件的宽和高,首相需要调用measure方法,然后用getMeasuredWidth()和getMeasuredHeight()取得宽和高。



你可能感兴趣的:(null)