4

目前我正在尝试使用以下代码设置我以编程方式创建的视图的位置:

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());

问题

我得到的错误是我不能在低于 11 的 api 级别中使用setLeftand属性。setTop

问题

如何以编程方式设置视图的位置API level < 11

4

1 回答 1

2

看起来您已经在创建自定义视图,因此您将覆盖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);
于 2012-08-13T14:31:58.827 回答