2014-09-22 91 views
1

我使用下面的代碼來定位的ImageViewImageView的動態位置

DisplayMetrics displayMetrics = new DisplayMetrics(); 
    getActivity().getWindowManager().getDefaultDisplay().getMetrics(displayMetrics); 
    int height = displayMetrics.heightPixels; 
    int width = displayMetrics.widthPixels; 

    bg = (ImageView) rootView.findViewById(R.id.rectimage); 
    bg.getLayoutParams().width = width; 
    bg.getLayoutParams().height = 500; 

    MarginLayoutParams params = (MarginLayoutParams)bg.getLayoutParams(); 
    params.setMargins(0, height - 510, 0, 0); 

我要永遠把ImageView的在熒屏,但screenHeight的底部 - IMAGE_HEIGHT不工作任何想法?

回答

1

嘗試添加

android:layout_alignParentBottom="true" 

您的ImageView在佈局xml文件

+0

希望這可以幫助你! – 2014-09-22 09:10:19

+0

謝謝你的工作 – sger 2014-09-22 09:24:13

+0

如果它適合你,那麼請接受我的回答 – 2014-09-22 09:24:44

1

這隻會工作,如果你的佈局是一個RelativeLayout的,因爲其他ViewGroups不明白alignParentBottom。

RelativeLayout layout = findViewById(R.id.my_relative_layout); 
ImageView imageView = new ImageView (this); // your button 
lp = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, 
LayoutParams.MATCH_PARENT); // You might want to tweak these to WRAP_CONTENT 
lp.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM); 
layout.addView(button, lp);