FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams((int)width, (int)height);
lp.width = (int) (width * 0.75f);
lp.height = (int) (height * 0.5f);
lp.setMargins((int) width/8, (int) height / 4, (int) (width - width/8) , (int) (height - height/4));
addContentView(img, lp);
这应该在屏幕中央显示我的 (ImageView) img。它在运行 API 16 的手机中按预期工作,但当我在模拟器中使用 API 8 进行调试时,img 显示在左上角。
我受够了尝试 RelativeLayout.LayoutParams.setMargins() 或 AddRule 在父 View 中居中对齐我的 View 。无法得到它。任何帮助表示赞赏。
我没有使用任何 XML 文件来扩充我的 View 组。我在我的 Activity 中以编程方式添加 View 。
这就是我想要的,适用于 OS 4.0。 &……第二张图片是 OS2.0 -> 这个我要修复。
最佳答案
修复:
lp.gravity = Gravity.FILL_HORIZONTAL;
关于android - FrameLayout.LayoutParams 在不同的 Android OS/API 上工作不同,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16730369/