我必须连续创建 Imageview 的动态列表,每个Imageview下还有一个其他Imageview(点Drawable)。 见下图,这是我想要的观点。
这是我的来源。
for (int i = 0; i < 9; i++) {
RelativeLayout.LayoutParams params1 = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
RelativeLayout.LayoutParams params2 = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
ImageView catImg = new ImageView(getApplicationContext());
catImg.setImageResource(bottDrawableArr[i]);
catImg.setId(View.generateViewId());
int dp = (int) utiles.convertDpToPixel(27, getApplicationContext());
params1.height = dp;
params1.width = dp;
ImageView dotImg = new ImageView(getApplicationContext());
dotImg.setImageResource(R.mipmap.dot);
dotImg.setId(View.generateViewId());
params2.addRule(RelativeLayout.BELOW, dotImg.getId());
dockBg.addView(catImg, params1);
dockBg.addView(dotImg, params2);
}
但我无法得到我的愿望,
虽然我的观点显示如下
我试图在每张图片下放置点但无法实现。
答案 0 :(得分:1)
最好的方法是使用一些LinearLayout
,如下所示:
LinearLayout layout = new LinearLayout(this);
layout.setOrientation(LinearLayout.HORIZONTAL);
for (int i = 0; i < 9; i++) {
LinearLayout layoutChild = new LinearLayout(this);
layoutChild.setOrientation(LinearLayout.VERTICAL);
RelativeLayout.LayoutParams params1 = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
RelativeLayout.LayoutParams params2 = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
ImageView catImg = new ImageView(getApplicationContext());
catImg.setImageResource(bottDrawableArr[i]);
catImg.setId(View.generateViewId());
int dp = (int) utiles.convertDpToPixel(27, getApplicationContext());
params1.height = dp;
params1.width = dp;
ImageView dotImg = new ImageView(getApplicationContext());
dotImg.setImageResource(R.mipmap.dot);
dotImg.setId(View.generateViewId());
params2.addRule(RelativeLayout.BELOW, dotImg.getId());
layoutChild.addView(catImg, params1);
layoutChild.addView(dotImg, params2);
layout.addView(layoutChild);
}
dockBg.addView(layout);
注意:最好删除不必要的属性,例如设置一些规则...
答案 1 :(得分:0)
params2.addRule(RelativeLayout.BELOW, dotImg.getId());
这行代码看起来像是在尝试添加规则以将dotImg
置于dotImg
之下。用catImg.getId()
替换它可以解决您的问题。
考虑使用RecyclerView可能是一个好主意,或者如果它是一个静态数组(总是相同的项目),那么你可以制作一个新的布局文件,并在需要时对其进行充气。