如何将堆栈布局设置为屏幕高度的一半?

时间:2016-07-22 05:08:32

标签: c# xamarin xamarin.forms

我正在努力确保动态加载的图片在我的应用程序中占用一致的垂直空间。显然,我希望在不定义静态高度的情况下执行此操作,以便我的视图可以缩放为更小/更大的设备。

我假设有一种方法可以告诉您希望它与其他堆叠布局共享空间的堆叠布局。

所以我试过的是:

    StackLayout _container, _top, _bottom;

    // left out irrelevant code

    _imageView = new CachedImage
    {
        HorizontalOptions = LayoutOptions.FillAndExpand,
        VerticalOptions = LayoutOptions.Start,
        DownsampleToViewSize = true,
        CacheDuration = TimeSpan.FromDays(5),
        TransparencyEnabled = false 
    };

    _top = new StackLayout { 
        BackgroundColor = Color.Red,
        HorizontalOptions = LayoutOptions.FillAndExpand,
        VerticalOptions = LayoutOptions.Start, 
        Children = { 
            _imageView
        }
    };
   _bottom = new StackLayout {
       BackgroundColor = Color.White,
       HorizontalOptions = LayoutOptions.FillAndExpand,
       VerticalOptions = LayoutOptions.End, 
       Children = {
           _oneThing,
           _twoThing
       }
   };

   _container = new StackLayout { 
   Padding = new Thickness (10),
   Spacing = 10,
   BackgroundColor = Color.Aqua,
   HorizontalOptions = LayoutOptions.FillAndExpand,
   VerticalOptions = LayoutOptions.FillAndExpand,
        Children = { 
            _top,
            _bottom 
        }
    };

    this.Content = _container;

不幸的是,这不起作用......

我的问题

如何确保我的堆栈视图(或仅我的图像)仅占视图的50%?

1 个答案:

答案 0 :(得分:1)

就像你提到的那样,你也可以使用Grid和一些RowDefinitions:

Grid mainLayout = new Grid
{
    VerticalOptions = LayoutOptions.FillAndExpand,
    RowDefinitions =
    {
        new RowDefinition { Height = new GridLength(1, GridUnitType.Star) }
        new RowDefinition { Height = new GridLength(1, GridUnitType.Star) }
    }
};

var _imageView = new Image
{
    HorizontalOptions = LayoutOptions.FillAndExpand,
    VerticalOptions = LayoutOptions.FillAndExpand
};

Grid.SetRow(_imageView, 0); //_imageView will be added to the first row

mainLayout.Children.Add(_imageView);