rod*_*ira 10 android android-layout android-constraintlayout
我有一个简单的布局:固定高度TextView,其中三个ImageView对象锚定在视图的底部.
如何以编程方式更改每个ImageView的高度,使它们在某个范围内变化(10dp,低于TextView 16dp)?具体来说,我的活动获得一个百分比列表(介于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);
Run Code Online (Sandbox Code Playgroud)
Che*_*amp 23
如果"高度"是指绝对的从上到下的尺寸,则ImageView
可以执行以下操作,使其高度增加一倍ImageView
.您可以将高度设置为所需的值.
ImageView iv = (ImageView) findViewById(R.id.imageView);
ConstraintLayout.LayoutParams lp = (ConstraintLayout.LayoutParams) iv.getLayoutParams();
lp.height = lp.height * 2;
iv.setLayoutParams(lp);
Run Code Online (Sandbox Code Playgroud)
请参阅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);
Run Code Online (Sandbox Code Playgroud)
请参见setVerticalBias.
还有其他方法可以移动ImageView
布局,但这取决于布局的设置方式.
归档时间: |
|
查看次数: |
12042 次 |
最近记录: |