【发布时间】:2017-09-08 08:12:39
【问题描述】:
我正在循环中动态创建多个 ImageView。 每个 ImageView 都有不同的位置和不同的图像。
最后我将它们添加到 FrameLayout。
FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(container.getWidth() / 3, container.getHeight() / 3);
ImageView imageView;
for(int i=0; i<cloths.size(); i++) {
imageView = new ImageView(getActivity(), null);
imageView.setAdjustViewBounds(true);
Glide.with(getActivity()).load(cloths.get(i).getImage()).into(imageView);
imageView.setOnTouchListener(touchListener);
params.leftMargin = (int) cloths.get(i).getxPos(container.getWidth());
params.topMargin = (int) cloths.get(i).getyPos(container.getHeight());
params.rightMargin = 0;
params.bottomMargin = 0;
imageView.setScaleY(cloths.get(i).getScale());
imageView.setScaleX(cloths.get(i).getScale());
container.addView(imageView, params);
}
不是正确定位所有图像视图,而是它们都在最后一个图像视图的位置上相互重叠。
任何想法如何解决它? 我做错了什么?
【问题讨论】:
-
您对每个具有不同边距的 ImageView 使用相同的
params。为什么要使用 FrameLayout? FrameLayout 可以在 9 个位置存储视图。更改边距不是更改视图位置的正确方法。 -
我使用 FrameLayout 是因为 ImageView 可以拖放!将图像视图添加到容器时,边距只是开始边距!
-
但是你给了我解决方案...在每次循环运行时创建新参数是解决方案!
标签: android loops dynamic imageview add