繁体   English   中英

如何对齐在类而不是xml文件中动态创建的imageView

[英]How to align imageViews which are created dynamically inside a class and not the xml file

我的问题:我想在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);
    }

试试这个:

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);
    }

合法值可以在https://developer.android.com/reference/android/view/View.html#setTextAlignment(int)中找到

但这对ImageView没有影响。 我不确定您实际上要做什么,所以我无法为您提供帮助。 我最好的猜测是您正在尝试以某种方式在父级布局中排列视图,但要对此进行帮助,我们需要知道父级的布局类型以及您要执行的操作。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM