Skip to content

Instantly share code, notes, and snippets.

@Axemasta
Last active February 5, 2018 10:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Axemasta/ac23cdcedd5a4385fa1caf3dbf24da95 to your computer and use it in GitHub Desktop.
Save Axemasta/ac23cdcedd5a4385fa1caf3dbf24da95 to your computer and use it in GitHub Desktop.
C# Code that demonstrates how you can add a button over a scroll view for a Xamarin app
using Xamarin.Forms;
using System;
namespace ScrollViewFormatting
{
public partial class ScrollViewFormattingPage : ContentPage
{
public ScrollViewFormattingPage()
{
InitializeComponent();
//Define UI
AbsoluteLayout absoluteLayout = new AbsoluteLayout();
RelativeLayout relativeLayout = new RelativeLayout();
ScrollView scrollView = new ScrollView();
BoxView scrollBox = new BoxView();
StackLayout stackView = new StackLayout();
Button myButton = new Button();
//Format UI
scrollBox.BackgroundColor = Color.Yellow;
scrollBox.HeightRequest = 2000;
scrollBox.HorizontalOptions = LayoutOptions.Fill;
stackView.Padding = new Thickness(10, 10, 10, 10);
stackView.Spacing = 20;
//Add alot of labels to the stack view (so the stackview's height is greater than the screens)
for (int i = 1; i < 30; i++) {
stackView.Children.Add(CreateLabel());
}
scrollView.Content = stackView;
//Add scrollview to relative layout
relativeLayout.Children.Add(scrollView,
xConstraint: Constraint.Constant(0),
yConstraint: Constraint.Constant(0),
widthConstraint: Constraint.RelativeToParent((parent) => { return parent.Width; }),
heightConstraint: Constraint.RelativeToParent((parent) => { return parent.Height; }));
//Format button
myButton.Text = "Press Me!";
myButton.BackgroundColor = Color.Black;
myButton.TextColor = Color.White;
myButton.HorizontalOptions = LayoutOptions.CenterAndExpand;
myButton.VerticalOptions = LayoutOptions.CenterAndExpand;
myButton.WidthRequest = 200;
AbsoluteLayout.SetLayoutBounds(myButton, new Rectangle(0.5, 0.95, 200, 50));
AbsoluteLayout.SetLayoutFlags(myButton, AbsoluteLayoutFlags.PositionProportional);
//Add views to layout & set as our view
absoluteLayout.Children.Add(relativeLayout);
absoluteLayout.Children.Add(myButton);
Content = absoluteLayout;
}
//Helper method to create alot of labels
public Label CreateLabel() {
Label localLabel = new Label();
localLabel.Text = "Sample Text...";
localLabel.TextColor = Color.Black;
localLabel.BackgroundColor = Color.LimeGreen;
return localLabel;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment