我有2个视图-容器视图和量规视图。我想以编程方式更改量规视图的高度。问题是,当我得到容器视图的高度时,即使高度为全屏,它也总是以2.0的形式返回!
int fullHeight = containerView.getLayoutParams().height;
Number height = fullHeight * (thisTank.getTankTotalVolume().doubleValue()/thisTank.getTankMaxVolume().doubleValue());
Number width = gaugeView.getWidth();
//gaugeView.setLayoutParams(new LinearLayout.LayoutParams(width.intValue(), height.intValue()));
gaugeView.getLayoutParams().height = height.intValue() * fullHeight;
gaugeView.requestLayout();
实际上,如果将视图的高度设置为WRAP_CONTENT
或MATCH_PARENT
in XML
,则其containerView.getLayoutParams().height
应等于2,这是默认值...!
您必须有两个选择,
- 首先,您可以在XML文件中定义一个修复值
- 其次,建议您为此使用树状观察器。请看下面的代码...!
final View layout = (View)findViewById(R.id.YOUR_VIEW_ID);
ViewTreeObserver vto = layout.getViewTreeObserver();
vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
this.layout.getViewTreeObserver().removeGlobalOnLayoutListener(this);
int width = layout.getMeasuredWidth();
int height = layout.getMeasuredHeight();
}
});
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句