Stick Layout in Xamarin Forms to bottom

后端 未结 7 1112
星月不相逢
星月不相逢 2020-12-12 19:11

I\'m making an application in Xamarin forms but I have some trouble sticking a layout to the bottom of the device. I thought an AbsoluteLayout would work, but I cannot grasp

相关标签:
7条回答
  • 2020-12-12 19:50

    Here's a class I use to automate this. There's plenty of additions you can make by extending the class to having a scrollable center section (so it doesn't overlap the bottom if too long) etc!

    public class CakeLayout : StackLayout
    {
        public CakeLayout()
        {
            TopStack = new StackLayout // TOP stack
            {
                Orientation = StackOrientation.Horizontal,
                VerticalOptions = LayoutOptions.Start
            };
    
            CenterStack = new StackLayout // CENTER stack
            {
                VerticalOptions = LayoutOptions.CenterAndExpand
            };
    
            BottomStack = new StackLayout // BOTTOM stack
            {
                Orientation = StackOrientation.Horizontal,
                VerticalOptions = LayoutOptions.End
            };
    
            Children.Add(TopStack);
            Children.Add(CenterStack);
            Children.Add(BottomStack);
        }
    
        public StackLayout BottomStack { get; private set; }
        public StackLayout CenterStack { get; private set; }
        public StackLayout TopStack { get; private set; }
    }
    

    Then to use this as a page, for example:

    public class MyPage
    {
        public MyPage()
        {
            CakeLayout cakeLayout = new CakeLayout();
    
            cakeLayout.TopStack.Children.Add(new Label { Text = "Hello Cake" });   
            cakeLayout.CenterStack.Children.Add(MyListView);
            cakeLayout.BottomStack.Children.Add(MyButton);
    
            // Assign the cake to the page
            this.Content = cakeLayout;
            ...
        }
    ...
    }
    
    0 讨论(0)
提交回复
热议问题