如何在约束布局中动态设置 ImageView 的位置
How can I set an ImageView's position dynamically in Constraint Layout
我在 ConstraintLayout
中有一个动态创建的 ImageView
。一旦我 运行 应用程序,ImageView 就会显示在左上角,因为没有为 ImageView
.
定义位置
如何动态设置 ImageView 的位置(比如 CENTER)?
我写了下面的代码:
ConstraintLayout layout = (ConstraintLayout) findViewById(R.id.constraintLayout);
ImageView imageView = new ImageView(ChooseOptionsActivity.this);
imageView.setImageResource(R.drawable.redlight);
layout.addView(imageView);
setContentView(layout);
非常感谢任何建议。
您需要使用应用于 ImageView
的 ConstraintSet
使其居中。 ConstraintSet
的文档可以在 here.
中找到
This class allows you to define programmatically a set of constraints to be used with ConstraintLayout. It lets you create and save constraints, and apply them to an existing ConstraintLayout. ConstraintsSet can be created in various ways...
也许这里最棘手的事情是视图如何居中。 here.
对居中技术的一个很好的描述
对于您的示例,以下代码就足够了:
// Get existing constraints into a ConstraintSet
ConstraintSet constraints = new ConstraintSet();
constraints.clone(layout);
// Define our ImageView and add it to layout
ImageView imageView = new ImageView(this);
imageView.setId(View.generateViewId());
imageView.setImageResource(R.drawable.redlight);
layout.addView(imageView);
// Now constrain the ImageView so it is centered on the screen.
// There is also a "center" method that can be used here.
constraints.constrainWidth(imageView.getId(), ConstraintSet.WRAP_CONTENT);
constraints.constrainHeight(imageView.getId(), ConstraintSet.WRAP_CONTENT);
constraints.center(imageView.getId(), ConstraintSet.PARENT_ID, ConstraintSet.LEFT,
0, ConstraintSet.PARENT_ID, ConstraintSet.RIGHT, 0, 0.5f);
constraints.center(imageView.getId(), ConstraintSet.PARENT_ID, ConstraintSet.TOP,
0, ConstraintSet.PARENT_ID, ConstraintSet.BOTTOM, 0, 0.5f);
constraints.applyTo(layout);
我在 ConstraintLayout
中有一个动态创建的 ImageView
。一旦我 运行 应用程序,ImageView 就会显示在左上角,因为没有为 ImageView
.
如何动态设置 ImageView 的位置(比如 CENTER)?
我写了下面的代码:
ConstraintLayout layout = (ConstraintLayout) findViewById(R.id.constraintLayout);
ImageView imageView = new ImageView(ChooseOptionsActivity.this);
imageView.setImageResource(R.drawable.redlight);
layout.addView(imageView);
setContentView(layout);
非常感谢任何建议。
您需要使用应用于 ImageView
的 ConstraintSet
使其居中。 ConstraintSet
的文档可以在 here.
This class allows you to define programmatically a set of constraints to be used with ConstraintLayout. It lets you create and save constraints, and apply them to an existing ConstraintLayout. ConstraintsSet can be created in various ways...
也许这里最棘手的事情是视图如何居中。 here.
对居中技术的一个很好的描述对于您的示例,以下代码就足够了:
// Get existing constraints into a ConstraintSet
ConstraintSet constraints = new ConstraintSet();
constraints.clone(layout);
// Define our ImageView and add it to layout
ImageView imageView = new ImageView(this);
imageView.setId(View.generateViewId());
imageView.setImageResource(R.drawable.redlight);
layout.addView(imageView);
// Now constrain the ImageView so it is centered on the screen.
// There is also a "center" method that can be used here.
constraints.constrainWidth(imageView.getId(), ConstraintSet.WRAP_CONTENT);
constraints.constrainHeight(imageView.getId(), ConstraintSet.WRAP_CONTENT);
constraints.center(imageView.getId(), ConstraintSet.PARENT_ID, ConstraintSet.LEFT,
0, ConstraintSet.PARENT_ID, ConstraintSet.RIGHT, 0, 0.5f);
constraints.center(imageView.getId(), ConstraintSet.PARENT_ID, ConstraintSet.TOP,
0, ConstraintSet.PARENT_ID, ConstraintSet.BOTTOM, 0, 0.5f);
constraints.applyTo(layout);