目前我正在尝试使用以下代码设置我以编程方式创建的 View 的位置:
LayoutParams params = bottomBar.getLayoutParams();
params.height = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,(float) 5, getResources().getDisplayMetrics());
params.width = LayoutParams.MATCH_PARENT;
bottomBar.setLayoutParams(params);
bottomBar.setLeft(0);
bottomBar.setTop(this.getHeight()-bottomBar.getHeight());
问题
我得到的错误是我无法使用 setLeft
和 setTop
API 级别低于 11 的属性。
问题
如何以编程方式设置 View 在 API level < 11
中的位置
最佳答案
看起来您已经在创建自定义 View ,因此您将覆盖 onLayout()
并调用 View#layout(int left, int top, int right, int bottom)
在你想要的布局上。
final int left = 0;
final int top = getHeight() - bottomBar.getHeight();
final int right = left + bottomBar.getWidth();
final int bottom = top + bottomBar.getHeight();
bottomBar.layout(left, top, right, bottom);
关于android - 以编程方式在 API 级别 7 中设置 View 位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11936279/