How can I set negative percent margins in PercentRelativeLayout?

1.6k Views Asked by At

I need to set negative margin in PercentRelativeLayout but when I try it in xml as follows it doesn't work.

app:layout_marginTopPercent="-10%"

I tried setting negative margin programmatically when parent is measured but PercentRelativeLayout preserves its children LayoutParams and reverts any changes made to them.

Setting negative margin in dp works as follows but I absolutely need to give a negative margin relative to parent size.

android:layout_margin="-10dp"

How can I do it, any ideas?

2

There are 2 best solutions below

0
On

I ended up translating the view with negative value depending on parent size when parent is measured. It's not a solid solutions but It works in my case.

protected void onSizeChanged(int w, int h, int oldw, int oldh) {
    super.onSizeChanged(w, h, oldw, oldh);
    float marginTop = h / -10.0f;
    child.setTranslationY(marginTop);
}
0
On

PercentRelativeLayout using -1.0f as default value of margins and the Helper only set a value >= 0.0f It's not necessary to use this Layout at your case; you can set the marginTopPercent to -1.0f and put the value in java with MarginLayoutParams by yourself