2016-12-26 66 views
0
頂部

我試圖以編程方式設置Xÿ一個RelativeLayout內的ImageView的位置。如何programmaticaly把ImageView的到的RelativeLayout

我可以設置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

enter image description here

有誰知道什麼是我的錯誤?

回答

1

添加這樣

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); 
1
lp.addRule(RelativeLayout.ALIGN_PARENT_TOP); add this line 
相關問題