如何以编程方式将 ImageView 设置到 RelativeLayout 的顶部

How to programmaticaly put ImageView to the top of RelativeLayout

我正在尝试以编程方式设置 RelativeLayoutImageViewXY 位置.

我可以设置 X 位置,但 Y 位置似乎不起作用。 ImageView 几乎显示在屏幕中间。

我在 Whosebug 上阅读了很多帖子并尝试了很多代码,但没有一个有效。

我给你看我的原始代码,没有展示我所有的尝试。

ViewGroup.LayoutParams viewlp = new ViewGroup.LayoutParams(
                                ViewGroup.LayoutParams.WRAP_CONTENT,
                                ViewGroup.LayoutParams.WRAP_CONTENT);

    viewlp.height = drawableHeight;
    viewlp.width = drawableWidth;
    imageView.setX(Xposition);
    imageView.setY(Yposition);
    imageView.setLayoutParams(viewlp);



    RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
            RelativeLayout.LayoutParams.WRAP_CONTENT, 
            RelativeLayout.LayoutParams.WRAP_CONTENT);
    lp.addRule(RelativeLayout.ALIGN_BOTTOM);

    layout.addView(imageView, lp);

在这里你可以看到我必须把 ImageView.

放在哪里

有人知道我的错误是什么吗?

lp.addRule(RelativeLayout.ALIGN_PARENT_TOP); add this line 

这样添加

RelativeLayout rl = new RelativeLayout(activity);
ImageView imgeView = new ImageView(activity);


RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams
            (LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
lp.addRule(RelativeLayout.ALIGN_PARENT_TOP);

rl.addView(imgeView, lp);