ImageView动态位置

时间:2014-09-22 09:00:29

标签: android imageview

我使用以下代码定位ImageView

    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始终放在屏幕的底部,但是屏幕高度 - image_height无效吗?

2 个答案:

答案 0 :(得分:1)

尝试添加

android:layout_alignParentBottom="true"

到布局xml文件中的ImageView

答案 1 :(得分:1)

这仅适用于您的布局是RelativeLayout,因为其他ViewGroup不了解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);