我正在使用以下代码添加图像并从左侧留出边距。
layoutParams.setMargins(translateBy, 0, 0, marginBottom);
slideIcon.setLayoutParams(layoutParams);
此代码适用于所有不同密度和大小的设备。
但这不适用于具有属性的设备:- 540x960, 240dpi, 4.7 英寸设备
但是如果我们从 .xml 文件中给出边距,那么它就可以工作了。
请帮我解决这个问题。
提前致谢。
最佳答案
您可以使用DisplayMetrics逻辑
A structure describing general information about a display, such as its size, density, and font scaling.
DisplayMetrics metrics = getResources().getDisplayMetrics();
int DeviceTotalWidth = metrics.widthPixels;
int DeviceTotalHeight = metrics.heightPixels;
layoutParams.setMargins(DeviceTotalWidth/8, 0, 0, marginBottom); // left, top, right, bottom
// DeviceTotalWidth/8 set yours left margin
已编辑
@Courtesy 转到#Stanojkovic
LayoutParams params = new LayoutParams( LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT ); params.setMargins(left, top, right, bottom);
希望这对您有所帮助。
关于android - 左边距不适用于 java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35059019/