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

来源:互联网 发布:usb高速数据采集卡 编辑:程序博客网 时间:2024/05/22 10:32

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()取得宽和高。


0 0