【问题标题】:Adding a margin to ImageView programmatically以编程方式向 ImageView 添加边距
【发布时间】:2012-11-05 04:15:47
【问题描述】:

我有一个表格布局,其中每一行都是以编程方式构建的。

结构基本上是,对于每一行:

TextView、LinearLayout、LinearLayout、LinearLayout、Button。

每个 LinearLayout 内部都有多个 ImageView。我想增加 ImageView 项目之间的间距,因为它们的边界是接触的。

我在这里尝试了建议 - In Android, how to make space between LinearLayout children? - 我这样创建参数:

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

并在添加控件时将其设置为图像视图:

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

请注意,我只将它设置在中间项目上作为左右边距,这对于我想要实现的目标来说很好。

问题是,即使没有在布局参数上设置任何边距,即只是实例化它并如上所示设置它,它会在左侧增加大约 35px 的边距,从而导致比我想要的大得多的边距。即使用 1px 调用 SetMargins 也不会改变边距。

我哪里错了?

【问题讨论】:

  • 尝试设置宽度wrapcontent。 (LinearLayout.LayoutParams.WrapContent, LinearLayout.LayoutParams.WrapContent)
  • @xitij 谢谢。请将此作为答案提交。

标签: android android-layout


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-26
    • 2020-04-20
    相关资源
    最近更新 更多