简体   繁体   中英

how to set left and top margin to a frame layout programmatically?

I have a framelayout inside a relative layout and i want to move frame layout a bit to the left and top as a whole. I have been using setLeft() and setTop() method. It is stretching the view but still it is okay for my purpose. My problem is I want the original unstretched frame layout back when I want it. But setting setRight() and putting the same parameter doesn't seem to work. Please help.

You can use setMargin to your framelayout

val par = framelayout?.layoutParams as FrameLayout.LayoutParams

par.rightMargin = 10
par.leftMargin = 30

You can't set Margin to your view like set padding. You need a Layout params for your frame layout then set margin to layout param.

FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT);
layoutParams.setMargins(5, 5, 5, 5);
frameLayout.setLayoutParams(layoutParams);

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