Skip to content

Instantly share code, notes, and snippets.

@vwart
Last active March 14, 2017 23:49
Show Gist options
  • Save vwart/4dd16f423fc6227df3707eaaf4d3464a to your computer and use it in GitHub Desktop.
Save vwart/4dd16f423fc6227df3707eaaf4d3464a to your computer and use it in GitHub Desktop.
This can be used to re-layout a Grid with star (*) rows within a ScrollView. For Xamarin Forms
//usage:
// ContactGrid is the x:Name of the Grid within the ScrollView
protected override void OnAppearing()
{
base.OnAppearing();
ContactGrid.LayoutStars(((float)App.INSTANCE.Resources["PageHeight"]));
}
//In your MainActivity:
private void InitAppResources()
{
App.INSTANCE.Resources["StatusBarHeight"] = GetStatusBarHeight() / Resources.DisplayMetrics.Density;
App.INSTANCE.Resources["NavBarHeight"] = GetNavBarHeight() / Resources.DisplayMetrics.Density;
App.INSTANCE.Resources["screenHeight"] = Resources.DisplayMetrics.HeightPixels / Resources.DisplayMetrics.Density;
App.INSTANCE.Resources["screenWidth"] = Resources.DisplayMetrics.WidthPixels / Resources.DisplayMetrics.Density;
App.INSTANCE.Resources["screenDensity"] = Resources.DisplayMetrics.Density;
App.INSTANCE.Resources["PageHeight"] = (Resources.DisplayMetrics.HeightPixels - GetStatusBarHeight() - GetNavBarHeight()) / Resources.DisplayMetrics.Density;
}
private int GetNavBarHeight()
{
int resourceId = Resources.GetIdentifier("navigation_bar_height", "dimen", "android");
if (resourceId > 0)
{
return Resources.GetDimensionPixelSize(resourceId);
}
return 0;
}
private int GetStatusBarHeight()
{
int resourceId = Resources.GetIdentifier("status_bar_height", "dimen", "android");
if (resourceId > 0)
{
return Resources.GetDimensionPixelSize(resourceId);
}
return 0;
}
//In your AppDelegate:
private void InitAppResources()
{
App.INSTANCE.Resources["statusBarHeight"] = (float)(40 / UIScreen.MainScreen.NativeScale);
App.INSTANCE.Resources["navBarHeight"] = (float)(88 / UIScreen.MainScreen.NativeScale);
App.INSTANCE.Resources["screenHeight"] = (float)(UIScreen.MainScreen.NativeBounds.Height / UIScreen.MainScreen.NativeScale);
App.INSTANCE.Resources["screenWidth"] = (float)(UIScreen.MainScreen.NativeBounds.Width / UIScreen.MainScreen.NativeScale);
App.INSTANCE.Resources["screenDensity"] = (float)UIScreen.MainScreen.NativeScale;
App.INSTANCE.Resources["pageHeight"] = (float)((UIScreen.MainScreen.NativeBounds.Height - 40 - 88) / UIScreen.MainScreen.NativeScale);
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xamarin.Forms;
namespace ProfiApp.Utils
{
public static class GridLayouter
{
public static double LayoutStars(this Grid g, double desiredHeight, int minStarHeight = 0)
{
double gridMinHeight = 0, finalHeight = 0;
int starCount = 0;
// start with padding
gridMinHeight += g.Padding.Top + g.Padding.Bottom;
gridMinHeight += g.RowSpacing * g.RowDefinitions.Count - 1;
// no look for star rows
List<RowDefinition> starRows = new List<RowDefinition>();
foreach(var rowDef in g.RowDefinitions)
{
if(rowDef.Height.IsAbsolute)
{
gridMinHeight += rowDef.Height.Value;
}
else if(rowDef.Height.IsStar)
{
starCount += (int)rowDef.Height.Value;
starRows.Add(rowDef);
}
else if(rowDef.Height.IsAuto)
{
//ToDo implement Auto
}
}
double starHeight = (desiredHeight - gridMinHeight) / starCount;
if (starHeight < minStarHeight)
starHeight = minStarHeight;
finalHeight = gridMinHeight;
foreach (var starDef in starRows)
{
starDef.Height = new GridLength(starDef.Height.Value * starHeight, GridUnitType.Absolute);
finalHeight += starDef.Height.Value;
}
return finalHeight;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment