我是Android开发的新手。所以,我很难做到这一点。我的活动屏幕(最好是相对布局)需要在显示屏上有一个下边距(比如1 dp)。我怎么能以编程方式执行此操作? 感谢
答案 0 :(得分:0)
您可以使用android:layout_marginBottom="1dp"
添加下边距。
以编程方式执行...
// Setup your RelativeLayout
RelativeLayout yourLayout = (RelativeLayout) findViewById(R.id.your_relative_layout);
// Set the margin
MarginLayoutParams params = (MarginLayoutParams) yourLayout.getLayoutParams();
yourLayout.bottomMargin = 1; // This sets the bottom margin to 1px
yourLayout.setLayoutParams(params);
如果要以像素为单位进行设置,请将yourLayout.bottomMargin = 1;
替换为:
yourLayout.bottomMargin = dpToPixels(1);
并添加以下方法:
private int dpToPixels(int dps) {
if (dps == 0) {
return 0;
}
final float scale = getResources().getDisplayMetrics().density;
return (int) (dps * scale + 0.5f);
}