以编程方式在API级别7中设置视图的位置

时间:2012-08-13 14:22:37

标签: android android-layout android-view android-compatibility

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

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级别使用setLeftsetTop属性。

问题

如何在API level < 11

中以编程方式设置视图的位置

1 个答案:

答案 0 :(得分: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);