我正在编写一个在按钮顶部绘制布局的方法。我的问题是我在较低的API上创建的布局的位置。在较新的版本中,我使用了setX()和setY()方法,但由于在较低的版本上不起作用,我尝试按照这里所述的Android - Use of view.setX() and setY in api 8来设置布局参数,但是我没有得到我想要的结果。有了这个
int[] location = new int[2];
button.getLocationOnScreen(location);
int x = location[0];
int y = location[1];
borderRelativeLayout.setX(x);
borderRelativeLayout.setY(y);
viewGroup.addView(borderRelativeLayout)我正在实现这个目标:correct position
但是如果我使用这段代码来支持更低的API
relativeLayoutparams.leftMargin = x;
relativeLayoutparams.topMargin = y;
viewGroup.addView(borderRelativeLayout, relativeLayoutparams);我得到的结果是:incorrect
任何帮助都将不胜感激。
发布于 2015-12-18 19:50:37
对于API 1:
int[] location = new int[2];
button.getLocationOnScreen(location);
int x = location[0];
int y = location[1];
borderRelativeLayout.offsetLeftAndRight(x - borderRelativeLayout.getLeft());
borderRelativeLayout.offsetTopAndBottom(y - borderRelativeLayout.getTop());编辑:
你有没有考虑过为了达到想要的效果而画一个背景?
https://stackoverflow.com/questions/34354919
复制相似问题