我的问题:我想在onCreate()方法中创建一个for循环来动态保存imageViews。我能够正确地执行所有操作,只是imageView显示在最左上角,即我无法为其分配对齐方式。
我的代码:
RelativeLayout layout = findViewById(R.id.relativeLayout);
for(int i=0;i<3;i++)
{
ImageView image = new ImageView(this);
image.setMaxHeight(200);
image.setMaxWidth(200);
//CODE TO ADD THE ALIGNMENT OF THE IMAGE
image.setImageResource(R.drawable.red);
layout.addView(image);
}
答案 0 :(得分:1)
尝试这个:
for(int i=0;i<3;i++)
{
ImageView image = new ImageView(this);
image.setMaxHeight(200);
image.setMaxWidth(200);
//CODE TO ADD THE ALIGNMENT OF THE IMAGE
image.setImageResource(R.drawable.red);
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(RelativeLayout.MarginLayoutParams.MATCH_PARENT, 200);
layoutParams.setMargins(40, 40, 40, 40);
image.setLayoutParams(layoutParams);
layout.addView(image);
}
答案 1 :(得分:0)
法律价值可在https://developer.android.com/reference/android/view/View.html#setTextAlignment(int)
中找到但这对ImageView没有影响。我不确定您实际上要做什么,所以我无法为您提供帮助。我最好的猜测是您正试图以某种方式在父级布局中排列视图,但要对此进行帮助,我们需要知道父级的布局类型以及您要执行的操作。