Fragment中测量宽度、高度, Caused by: java.lang.IllegalArgumentException: width and height must be > 0
来源:互联网 发布:淘宝如何设为公益宝贝 编辑:程序博客网 时间:2024/05/29 08:59
在Fragment中测量控件的宽高,报错 Caused by: java.lang.IllegalArgumentException: width and height must be > 0:
对于Fragment可以采用下面的方法:
1. 使用 ViewTreeObserver 提供的 Hook 方法。
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_welcome);
myButton = (Button) findViewById(R.id.button1);
// 向 ViewTreeObserver 注册方法,以获取控件尺寸
ViewTreeObserver vto = myButton.getViewTreeObserver();
vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
public void onGlobalLayout() {
int h = myButton.getHeight();
Log.i(TAG, "Height=" + h); // 得到正确结果
// 成功调用一次后,移除 Hook 方法,防止被反复调用
// removeGlobalOnLayoutListener() 方法在 API 16 后不再使用
// 使用新方法 removeOnGlobalLayoutListener() 代替
myButton.getViewTreeObserver().removeGlobalOnLayoutListener(this);
}
});
// ...
}
该方法在 onGlobalLayout() 方法将在控件完成绘制后调用,因而可以得到正确地结果。该方法在 Fragment 中,也可以使用。
解决办法:
// 向 ViewTreeObserver 注册方法,以获取控件尺寸final ViewTreeObserver vto = iv_two_code.getViewTreeObserver();vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() { public void onGlobalLayout() { int height = iv_two_code.getHeight(); int width = iv_two_code.getWidth(); // Log.i(TAG, "Height=" + h); // 得到正确结果 // 成功调用一次后,移除 Hook 方法,防止被反复调用 // removeGlobalOnLayoutListener() 方法在 API 16 后不再使用 // 使用新方法 removeOnGlobalLayoutListener() 代替 iv_two_code.getViewTreeObserver().removeGlobalOnLayoutListener(this); }});
- Fragment中测量宽度、高度, Caused by: java.lang.IllegalArgumentException: width and height must be > 0
- java.lang.IllegalArgumentException: width and height must be > 0
- Bitmap.createBitmap java.lang.IllegalArgumentException: width and height must be > 0 问题解决
- Bitmap.createBitmap java.lang.IllegalArgumentException: width and height must be > 0 问题解决
- Bitmap.createBitmap参数异常之java.lang.IllegalArgumentException: width and height must be > 0
- bitmap 缩放 java.lang.IllegalArgumentException: width and height must be > 0
- Caused by: java.lang.IllegalArgumentException: Original must not be null
- Caused by: java.lang.IllegalArgumentException: Service Intent must be explicit:
- java.lang.IllegalArgumentException: Width (0) and height (560) cannot be <= 0
- Caused by: java.lang.IllegalArgumentException: At least one JPA metamodel must be present!
- Android报错:Caused by: java.lang.IllegalArgumentException: Service Intent must be explicit: Intent
- Caused by: java.lang.IllegalArgumentException: At least one JPA metamodel must be present!
- Caused by: java.lang.IllegalArgumentException: Text may not be null
- java.lang.IllegalArgumentException: x + width must be <= bitmap.width()问题
- createBitmap — java.lang.IllegalArgumentException: x must be < bitmap.width()错误的解决
- Caused by: java.lang.IllegalArgumentException: The handle attribute is must refer to an existing chi
- Caused by: java.lang.IllegalArgumentException: you must specify a way to create the tab indicator.
- android Caused by: java.lang.IllegalArgumentException: Binary XML file line #7: Must specify unique
- codeforces 263A(Beautiful Matrix) Java
- ubuntu下安装nginx时依赖库zlib,pcre,openssl安装方法
- 5-4 是否同一棵二叉搜索树
- php获取textarea的值作为邮箱内容发出去不换行的问题处理,即回车换行的方法
- Opencv学习之图像的矩
- Fragment中测量宽度、高度, Caused by: java.lang.IllegalArgumentException: width and height must be > 0
- less中使用calc
- TextView属性大全
- Interface RowMapper 简介(译自spring 官方文档)
- 如何用好 github 中的 watch、star、fork
- QuickUnity —— 提高Unity开发者开发效率的利器
- PKI基础知识
- 飞行器轨道仿真
- android 5.0预制APK方法,android5.0预制apk