【问题标题】:Setting Position of View in API Level 7 Programmatically以编程方式在 API 级别 7 中设置视图位置
【发布时间】:2012-08-09 19:10:31
【问题描述】:

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

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中的位置

【问题讨论】:

  • 问题需要更好地概述,你想达到什么目的?也许一个简单的布局图会有所帮助。如果您想将某些内容与屏幕底部对齐,请考虑使用等价的 alignParentBottom="true" 的相对布局
  • 查看 [this answer][1] ,直截了当 [1]:stackoverflow.com/questions/12195768/…

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


【解决方案1】:

看起来您已经在创建自定义视图,因此您将覆盖 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);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-06-15
    • 2017-09-25
    • 1970-01-01
    • 1970-01-01
    • 2014-08-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多