简体   繁体   English

以编程方式向ImageView添加边距

[英]Adding a margin to ImageView programmatically

I have a table layout where each row is built programmatically. 我有一个表格布局,其中每一行都以编程方式构建。

The structure is basically, for each row: 对于每一行,结构基本上是:

TextView, LinearLayout, LinearLayout, LinearLayout, Button. TextView,LinearLayout,LinearLayout,LinearLayout,Button。

Each of the LinearLayout has multiple ImageViews inside of them. 每个LinearLayout内部都有多个ImageView。 I want to increase the spacing between the ImageView items as they render with their borders touching. 我想增加ImageView项之间的间距,因为它们会触摸边界。

I tried the suggestions here - In Android, how to make space between LinearLayout children? 我在这里尝试了建议- 在Android中,如何在LinearLayout子项之间腾出空间? - I create the parameters like so: -我像这样创建参数:

LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FillParent, LinearLayout.LayoutParams.WrapContent);

And set it like this to an image view when adding the control: 并在添加控件时将其设置为图像视图:

linearLayout1.AddView(image1);
linearLayout1.AddView(image2, layoutParams);
linearLayout1.AddView(image3);

Note I am only setting it on the middle item as a left & right margin on this would be fine for what I am trying to achieve. 请注意,我仅将其设置在中间项目上,这样就可以达到我想达到的目的的左右边距。

The problem is that even without setting any margins on the layout params ie just instantiating it and setting it as shown above, it adds about a 35px margin to the left causing a much larger margin than I wanted. 问题是,即使没有在布局参数上设置任何边距(即仅实例化并按上面所示进行设置),它也会在左侧增加约35px的边距,从而导致比我想要的要大得多的边距。 Even calling SetMargins with 1px doesn't change the margin. 即使以1px调用SetMargins也不会更改边距。

Where am I going wrong? 我要去哪里错了?

像这样将线性布局的宽度设置为WrapContent

LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WrapContent, LinearLayout.LayoutParams.WrapContent);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM