dumazy
dumazy

Reputation: 14435

Set negative margin on View programmatically

I know how to set margins of a View programmatically with LinearLayout.LayoutParams and the method setMargins(int, int, int, int) but how can I put a negative margin on a view?

Upvotes: 7

Views: 5811

Answers (3)

Ali Bagheri
Ali Bagheri

Reputation: 3419

use this

params.setMargins(0,5-10,0,0);

no

params.setMargins(0,-5,0,0);

Upvotes: 0

Pat Myron
Pat Myron

Reputation: 4638

Using math seems to trick it enough for me.

 ViewGroup.MarginLayoutParams params =
     (ViewGroup.MarginLayoutParams)view.getLayoutParams();
 params.topMargin = 100 - 200;   // -100

Upvotes: 0

laalto
laalto

Reputation: 152817

Access the layout params for your parent layout and modify them as you like:

 ViewGroup.MarginLayoutParams params =
         (ViewGroup.MarginLayoutParams)view.getLayoutParams();
 params.topMargin = ...; // etc
 // or
 params.setMargins(...);

After you've modified the layout, call view.requestLayout().

Upvotes: 10

Related Questions