如标题所述,我试图在一个ConstraintLayout中以编程方式设置多个ImageViews的大小和位置百分比。
我成功地通过ConstraintSet或ConstraintLayout.LayoutParams进行了单独操作,但当我试图同时进行这两项操作时,似乎没有任何效果。
我还试着用Guidelines来表示位置,用ConstraintSet来表示大小,但还是没有结果。
下面是我想实现这一目标的for循环(位置工作,不知道如何做大小)。
ConstraintLayout layout = findViewById(R.id.mapView);
for(Point p : getCoords()){
ImageView iv = new ImageView(this);
iv.setId(View.generateViewId());
iv.setLayoutParams(getFeatureLayout(p.x/100f, p.y/100f));
iv.setImageDrawable(getResources().getDrawable(R.drawable.area));
layout.addView(iv);
getFeatureLayout方法如下
private ConstraintLayout.LayoutParams getFeatureLayout(float percentFromLeft, float percentFromTop){
assert percentFromTop>=0 && percentFromTop<=1 : "Inputs must be 0<=x<=1";
assert percentFromLeft>=0 && percentFromLeft<=1 : "Inputs must be 0<=x<=1";
ConstraintLayout.LayoutParams params = new ConstraintLayout.LayoutParams(
ConstraintProperties.WRAP_CONTENT,
ConstraintProperties.WRAP_CONTENT
params.bottomToBottom= ConstraintProperties.PARENT_ID;
params.topToTop= ConstraintProperties.PARENT_ID;
params.startToStart= ConstraintProperties.PARENT_ID;
params.endToEnd= ConstraintProperties.PARENT_ID;
params.verticalBias=percentFromTop;
params.horizontalBias=percentFromLeft;
return params;