简体   繁体   中英

Changing margin programmatically using sdp

I am using compile 'com.intuit.sdp:sdp-android:1.0.3' api for setting margins of linear layout like this

<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>

Now when I change the margin programatically, it changes it in dp not in 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);

So how to change the margin programatically according to sdp? Thank you

You can try

For Java

int margin = getResources().getDimensionPixelSize(R.dimen._10sdp);
params.setMargins(0, margin, margin, 0);

For Kotlin as pointed out by Zohab Ali

resources.getDimensionPixelSize(R.dimen._10sdp)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM