【问题标题】:Auto width or height in xamarin formsxamarin 表单中的自动宽度或高度
【发布时间】:2017-07-02 06:00:49
【问题描述】:

我们知道在android中我们可以使用

android:width='wrap_content'
android:height='match_parent'

设置视图的宽度或高度。
但是,在 xamarin 形式中,我们不能使用它。

那么如何为我的 xamarin 表单组件设置动态宽度或高度?
喜欢match_parentwrap_content 在android中

【问题讨论】:

    标签: android xamarin xamarin.android xamarin.forms


    【解决方案1】:

    在 Xamarin.Forms 中,您可以使用 LayoutOptions 执行相同操作。

    假设你有一个 Button,当它的父级增长时你想水平增长(宽度),你使用:

    var button4 = new Button {Text = "Click Me!", HorizontalOptions=LayoutOptions.FillAndExpand, VerticalOptions=LayoutOptions.Center};
    

    LayoutOptions.FillAndExpand 意味着它将占用其父视图中所有可用的空间(有一些例外)。

    在 XAML 中应该是:

    <Button Text="Click Me!"
            HorizontalOptions="FillAndExpand"
            VerticalOptions="Center" />
    

    建议您查看 Xamarin Forms 的 Getting Started

    【讨论】:

    • 嗨 @apineda 感谢您的回答,我已经使用了您的解决方案并在 stacklayout 中工作,但是当我使用 relativelayout 时它失败了,这意味着按钮不想居中
    • 这是因为在 Android (java) 中使用 RelativeLayout 时,定位方法并不相同。 Xamarin.Forms 中的 RelativeLayout 指南可以在这里看到:developer.xamarin.com/guides/xamarin-forms/user-interface/…
    【解决方案2】:

    查看 Horizo​​ntalOptions 和 VerticalOptions 下的选项。听起来 FillAndExpand 就是您所追求的。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-10-31
      • 2017-03-02
      • 1970-01-01
      • 1970-01-01
      • 2018-05-26
      • 2013-02-07
      • 2016-06-20
      相关资源
      最近更新 更多