2017-10-05 131 views
0

我有一個簡單的佈局:一個固定高度的TextView,三個ImageView對象被錨定到視圖的底部。編程更改約束佈局中的高度?

enter image description here

我如何編程方式更改每個ImageViews的高度,使他們的一些範圍(10dp,16DP TextView的下面)內變化?具體來說,我的活動得到一個百分比列表(介於0和1之間),表示這些「酒吧」應占用多少垂直空間。

我已經嘗試沒有成功如下:

// No visible difference 
imgView.setMinHeight(newHeight); 

// No change to height, but horizontal constrained was messed up 
imgView.setLayoutParams(new LayoutParam(imgView.getWidth(), newHeight); 
+0

你會從你想要更改的元素中獲得ConstraintSet,並對它們進行調整。有關如何操作,請參閱此答案:https://stackoverflow.com/a/40028802/4232337 – NSimon

回答

3

如果「高度」你的意思是ImageView的絕對頂部到底部的尺寸,你可以做以下翻一番,達到的高度ImageView。您可以將高度設置爲所需的值。

ImageView iv = (ImageView) findViewById(R.id.imageView); 
ConstraintLayout.LayoutParams lp = (ConstraintLayout.LayoutParams) iv.getLayoutParams(); 
lp.height = lp.height * 2; 
iv.setLayoutParams(lp); 

請參閱ConstraintLayout.LayoutParams

但是,如果「高度」你的意思是ImageView的垂直位置,你可以做以下改變ImageView的垂直偏差(假設您正在使用ConstraintLaytout):

ConstraintSet cs = new ConstraintSet(); 
ConstraintLayout layout = (ConstraintLayout) findViewById(R.id.layout); 
cs.clone(layout); 
cs.setVerticalBias(R.id.imageView, 0.25f); 
cs.applyTo(layout); 

setVerticalBias

還有其他方法可以移動關於佈局的ImageView,但這取決於您的佈局如何設置。

相關問題