【问题标题】:How can I set Margin in Code-behind for MonoDroid?如何在 MonoDroid 的代码隐藏中设置边距?
【发布时间】:2012-11-28 05:00:33
【问题描述】:

这个问题很简单,但我什么也没找到。如何在代码隐藏中为小部件设置边距。

我在Xamarin's site 中找到了此文档,但我无法将其用于 ImageView

我也在 ImageView 中尝试了 Layout() 方法,但没有成功。

        ImageView imgView = FindViewById<ImageView>(Resource.Id.imageView);
        imgView.Layout(10, 10, 10, 10);

【问题讨论】:

标签: c# android android-layout xamarin.android xamarin


【解决方案1】:

您是否尝试过使用 FrameLayout 的 LayoutParams?

这是一个例子:

您的图片:

ImageView imgView = FindViewById<ImageView>(Resource.Id.imageView);

你的图片参数:

FrameLayout.LayoutParams imgViewParams = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.WrapContent, FrameLayout.LayoutParams.WrapContent, 0.0f);
imgViewParams.SetMargins(10, 10, 10, 10);

设置图像参数:

imgView.LayoutParameters = imgViewParams;

编辑:将 LinearLayout.LayoutParameters 更改为 FrameLayout.LayoutParameters!

【讨论】:

  • 我试图创建一个 LinearLayout.LayoutParams 并设置到 ImageView 中,但问题是我有一个框架布局而不是线性布局,我只是更改为 FrameLayout.LayoutParams 并且它可以工作,谢谢。
【解决方案2】:

你能在你的布局中添加一个 LinearLayout 吗?

您可以将 ImageView 包裹在其中并添加边距!

像这样:

<LinearLayout 
    android:orientation="horizontal"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_margin="10dp" >
    <ImageView
        your imageview stuff />
</LinearLayout>

【讨论】:

    【解决方案3】:

    不知道这是否有帮助,但您可以尝试像这样更改边距值,看看效果如何:

    imgView.margin = new thickness(leftvalue,topvalue,rightvalue,botvalue);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-11-03
      • 2018-08-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-08-07
      • 2021-06-07
      • 2012-02-24
      相关资源
      最近更新 更多