简体   繁体   English

如何以编程方式将左边距和上边距设置为框架布局?

[英]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.

相关问题 如何以编程方式设置 EditText 布局边距? - How to set EditText layout margin programmatically? 如何在Android中以编程方式为相对布局设置边距? - How to set Margin for Relative Layout programmatically in android? 如何以编程方式设置线性布局边距(Xamarin) - How to set Linear Layout Margin Programmatically (Xamarin) 如何仅设置布局的上边距 - how to set only top margin of a layout 如何在Android中动态设置弹出窗口的左边距,顶部右边距,右边距右边距和底部底部对齐 - How to set Margin Left ,Margin top,Margin right and Margin Bottom alignment for popup window dynamically in android 如何以编程方式将“布局”设置为“屏幕顶部”和“屏幕开始”? - How to programmatically set Layout to Top and Start of screen? 以编程方式更改线性布局上边距android - Change linear layout top margin programmatically android 如何在android中以编程方式为视图(不是viewGroup)设置layout_margin - how to set layout_margin for a view (not for a viewGroup) programmatically in android 如何在Android的框架布局中获得图像视图的左上角? - How to get left and top of image view in frame layout for android? ListView行在CustomAdapter中以编程方式设置左边距 - ListView row set Left Margin programmatically in CustomAdapter
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM