[英]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.我在相对布局中有一个
framelayout
布局,我想将框架布局整体向左和顶部移动一点。 I have been using setLeft()
and setTop()
method.我一直在使用
setLeft()
和setTop()
方法。 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.但是设置
setRight()
并放置相同的参数似乎不起作用。 Please help.请帮忙。
You can use setMargin to your framelayout您可以将 setMargin 用于您的框架布局
val par = framelayout?.layoutParams as FrameLayout.LayoutParams
par.rightMargin = 10
par.leftMargin = 30
You can't set Margin to your view like set padding.您不能像设置填充那样将 Margin 设置为您的视图。 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);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.