我正在尝试在Android应用中测试边距。我的测试如下:
public void testMargins(){
RelativeLayout.LayoutParams lp;
float expected = dpToPixels( 6.0f );
lp = (RelativeLayout.LayoutParams) celsius.getLayoutParams();
* assertEquals( expected,lp.leftMargin );
assertEquals( expected,lp.rightMargin );
lp = (RelativeLayout.LayoutParams) fahrenheit.getLayoutParams();
assertEquals( expected,lp.leftMargin );
assertEquals( expected,lp.rightMargin );
}
我正在将DP转换为像素,因为leftMargin
和rightMargin
以像素为单位。执行此转换的方法是:
public float dpToPixels(float dp){
float densityDpi = activity.getResources().getDisplayMetrics().densityDpi;
return dp * ( densityDpi / 160.0f );
}
但测试失败说:
junit.framework.AssertionFailedError: expected:<21.0> but was:<21>
在标有星号的行上。我尝试将“实际”值转换为浮动,但这没有帮助。
我该怎么做?