1

this question is simple, but i'm not finding nothing about it. How can i set margin into a widget in code-behind.

I found this document into Xamarin's site, but i can't use this into a ImageView

I also tried the method Layout() in my ImageView, but it didn't work.

        ImageView imgView = FindViewById<ImageView>(Resource.Id.imageView);
        imgView.Layout(10, 10, 10, 10);
Fabio Reis
  • 95
  • 1
  • 2
  • 11

3 Answers3

7

Have you tried using FrameLayout's LayoutParams?

Here is an example:

Your image:

ImageView imgView = FindViewById<ImageView>(Resource.Id.imageView);

Your image's parameters:

FrameLayout.LayoutParams imgViewParams = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.WrapContent, FrameLayout.LayoutParams.WrapContent, 0.0f);
imgViewParams.SetMargins(10, 10, 10, 10);

Setting your image's parameters:

imgView.LayoutParameters = imgViewParams;

EDIT: Changed LinearLayout.LayoutParameters to FrameLayout.LayoutParameters!

Ivozor
  • 976
  • 8
  • 23
  • 1
    i was trying to create a LinearLayout.LayoutParams and set into the ImageView, but the problem was that i had a Frame Layout instead of a Linear Layout, I just changed to FrameLayout.LayoutParams and it works, thanks. – Fabio Reis Dec 26 '12 at 13:39
1

Can you add a LinearLayout to your layout?

You could wrap your ImageView inside it and add margins!

Like this:

<LinearLayout 
    android:orientation="horizontal"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_margin="10dp" >
    <ImageView
        your imageview stuff />
</LinearLayout>
Ivozor
  • 976
  • 8
  • 23
0

Don't know if this helps but you could try changing margin values like this and see how well this works:

imgView.margin = new thickness(leftvalue,topvalue,rightvalue,botvalue);
johnnyRose
  • 7,310
  • 17
  • 40
  • 61
Jeff Jose
  • 131
  • 2
  • 2