簡體   English   中英

如何以編程方式將ImageView放在RelativeLayout的頂部

[英]How to programmaticaly put ImageView to the top of RelativeLayout

我正在嘗試以編程方式在RelativeLayout內設置ImageView XY位置。

我可以設置X位置,但Y位置似乎不起作用。 ImageView幾乎顯示在屏幕中間。

我在這里閱讀了許多關於stackoverflow的文章,並嘗試了許多代碼,但是沒有人可以使用。

我沒有顯示我的所有嘗試,而是顯示了我的原始代碼。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM