使用代码

时间:2018-01-25 22:54:45

标签: java android android-studio imageview

我在android studio的MainActivity中尝试了这段代码:

ImageView redBlock = new ImageView(this);
redBlock.setImageResource(R.drawable.blablabla);
redBLock.setVisibility(View.VISIBLE);
redBlock.setX(45f);
redBlock.setY(45f);

运行时,它不会显示在手机屏幕上

1 个答案:

答案 0 :(得分:0)

图像视图未包含在视图组中,因此不会出现 您可以引用ViewGroup,例如LinearLayout,ConstraintLayout或任何其他ViewGroup,然后将图像视图添加到它,例如。

    LinearLayout ll = findViewById(R.id.container);
    ImageView redBlock = new ImageView(this);
    redBlock.setImageResource(R.drawable.blablabla);
    redBLock.setVisibility(View.VISIBLE);
    redBlock.setX(45f);
    redBlock.setY(45f);
    ll.add(redBlock)