从dp转换为像素错误

Pra*_*mar 0 android android-layout

由于一些奇怪的原因,我无法从下面的函数得到正确的输出,我知道我做了一些愚蠢的错误,但不确定,

public static int convertDpToPixel(int dp, Context context){
       float px = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,dp,context.getResources().getDisplayMetrics());
        return Math.round(px);
    }
Run Code Online (Sandbox Code Playgroud)

而我正在调用函数 - gen_function.convertDpToPixel(R.dimen.button_left_right_margin,this);

输出是一个巨大的数字,这是不期望的,任何帮助非常赞赏.

Arp*_*kar 5

用这个

int pixels = getResources().getDimensionPixelSize(R.dimen.button_left_right_margin);
Run Code Online (Sandbox Code Playgroud)