android - 以编程方式在 API 级别 7 中设置 View 位置

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

目前我正在尝试使用以下代码设置我以编程方式创建的 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());

问题

我得到的错误是我无法使用 setLeftsetTop 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/

相关文章:

Android - 找不到 onButtonClick(View) 方法

android - 日期选择器和日历中的日期格式更改

android - View 的 setContentDescription() 的目的是什么?

android - 有没有一种方法可以在不使用可运行对象的情况下使线程的屏幕无效(更新)

android - 如何在 Handler android 中使用共享首选项

java - 异步 http 请求后更新 Fragment UI

android - setFocusable 不工作

java - 如何在android模拟器中显示来自URL的图像

java - 设置和更新 AlertDialog 的自定义 View

java - 如何以编程方式访问在 xml 中声明不可见的 View ?