Hello,
Welcome to our Microsoft Q&A platform!
Try getting the LayoutParameters
of the button, and then get the margins from the layout params. The value is in pixels, and you can use density to convert it to dp units.
Here is the sample code, you could refer to it.
var button = FindViewById<Button>(Resource.Id.btn);
button.Click += Button_Click;
var layoutParams = button.LayoutParameters as Android.Widget.RelativeLayout.LayoutParams;//the LayoutParams type is decided by the button's parent layout
var leftMargin = ConvertToDP(layoutParams.LeftMargin);
var rightMargin = ConvertToDP(layoutParams.RightMargin);
int ConvertToDP(int pxValue)
{
var density = Resources.DisplayMetrics.Density;
return (int)(pxValue / density);
}
Best Regards,
Jarvan Zhang
If the response is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.