4

此刻我想用下面的代码来设置我的编程方式创建视图的位置:设置视图的位置API 7级编程

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

的问题

错误我明白的是,我不能在API级别小于11

问题使用setLeftsetTop性能

如何编程设置API level < 11

+0

该问题需要做的更好,你想达到什么?也许一个简单的布局图会有所帮助。如果要对齐的东西在屏幕的底部,使用alignParentBottom =“真” – 2012-08-13 14:28:17

+0

退房[这个答案]相当于想想相对布局[1],它直截了当 [1]: http://stackoverflow.com/questions/12195768/android-use-of-view-setx-and-sety-in-api-8/28151869#28151869 – Orr 2015-01-26 14:21:27

回答

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