使用sdp以编程方式更改边距

she*_*hzy 2 android margin

我正在使用compile 'com.intuit.sdp:sdp-android:1.0.3'api来设置像这样的线性布局的边距

<LinearLayout
        android:id="@+id/layout_message"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="@dimen/_10sdp"
        android:layout_marginTop="@dimen/_10sdp"
        android:padding="@dimen/_8sdp"
        android:orientation="vertical">
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

现在,当我以编程方式更改边距时,它会在dp中更改它而不是在sdp中

LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
                    ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);

params.setMargins(0, 10, 10, 0);
layout_message.setLayoutParams(params);
Run Code Online (Sandbox Code Playgroud)

那么如何根据sdp以编程方式更改边距?谢谢

Jim*_*tel 9

你可以试试

对于Java

int margin = getResources().getDimensionPixelSize(R.dimen._10sdp);
params.setMargins(0, margin, margin, 0);
Run Code Online (Sandbox Code Playgroud)

对于Zothab Ali指出的Kotlin 来说

resources.getDimensionPixelSize(R.dimen._10sdp)
Run Code Online (Sandbox Code Playgroud)

  • 对于kotlin`resources.getDimensionPixelSize(R.dimen._10sdp)` (2认同)