2015-10-15 99 views
0

我有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(); 

回答

2

其实,如果你设置视图对WRAP_CONTENTMATCH_PARENT高度XMLcontainerView.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(); 

    } 
}); 
+0

这是伟大的谢谢!必须承认要努力改变到android开发! – HillInHarwich

+0

快乐兄弟.... :) –

相关问题