假设您要创建RelativeLayout的子类,并在类定义内获取其宽度和高度。(通过xml将子类添加到应用程序match_parent,match_parent)。
我试过下面的代码,但它为我敬酒0:
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
// TODO Auto-generated method stub
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
Toast.makeText(context, "" + this.getWidth(), Toast.LENGTH_SHORT).show();
}
这给了我-1:
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
// TODO Auto-generated method stub
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
Toast.makeText(context, "" + this.getLayoutParams().width, Toast.LENGTH_SHORT).show();
}
那么我有什么选择,或者onMeasure()
仅适用于anroid.view.View的子类?
编辑:我知道我可以打电话onWindowFocusChanged()
的Activity
,但我可以得到的大小RelativeLayout
子在其类的定义,所以,我并不需要夫妻两个班的,只是因为这个?
您可以覆盖onLayout方法或onSizeChanged。您也可以使用ViewTreeOserver。如果您想知道onMeasure方法的结果,请使用getMeasuredWidth/Height
代替getWidth/Height
onMeasure。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句