What's the proposed way to add space to layouts in Xamarin.Forms?
One way would be to add a Frame with no children like so:
new Frame {
BackgroundColor = Color.White,
HeightRequest = 1,
MinimumHeightRequest = 1,
HasShadow = false
}
Unfortunately, HeightRequest and MinimumHeightRequest get ignored.
Does a better way exist?
You could put your controls inside layouts (like frame, scroll view, stack panel) and use Padding property:
this.stackPanel = new StackLayout ()
{
Padding = new Thickness (8, 8)
};
var scrollView = new ScrollView ()
{
Content = stackPanel,
Padding = new Thickness (1, 2, 3, 4)
};
var frame = new Frame ()
{
Padding = new Thickness (8)
};
If you want space between two buttons for example, I believe this would do the trick. The first one adds 10 to bottom padding, the second adds 10 to top padding for total of 20.
var frame1 = new Frame ()
{
Padding = new Thickness (0,0,0,10),
Content = new Button()
};
var frame2 = new Frame ()
{
Padding = new Thickness (0,10,0,0),
Content = new Button()
};
Most Xamarin.Forms Layouts supports adding space between elements:
StackLayout has a Spacing property,
Grid has RowSpacing and ColumnSpacing properties,
...
Now, if you want to add spacing at a particular place, the way to to it is to include a BoxView:
myStackLayout.Children.Add (new BoxView {Color = Color.Transparent, HeightRequest = 5});
You can also wrap your content in a Frame or ContentView, but it adds padding to the content instead of adding space (although the effect will be the same).
What I do worked perfectly for me:
Suppose you want to distribute 2 Labels evenly on a horizontal StackLayout:
new StackLayout
{
Orientation = StackOrientation.Horizontal,
HorizontalOptions=LayoutOptions.CenterAndExpand,
Spacing = 0, // <- Very important!!
Children = {
new Label { Text = "Label 1" },
new BoxView { HorizontalOptions = LayoutOptions.FillAndExpand }, // <- the clever part
new Label { Text = "Label 2" }
}
};
Summary
By inserting BoxViews that fill the remaining space ("FillAndExpand") between your views, your views appear evenly distributed.
By setting Spacing = 0, you don't get extra space between your views.
Try:
myFrame.TranslateX=10;
myFrame.TranslateY=10;
I wanted to share a screenshot in conjunction with Lay González's answer but the edit queue was full->
To get dynamic spacing similar to CSS "Space-Between" in Xamarin you can insert filler views between your views that actually have content.
Here is an example:
Omit the filler view after the last "actual" view so that the view you want is at the end (the "-50" label at the bottom in the example).
Related
In xamarin I would like a label text with a maximum length. When they modify the size of the device screen, all the labels are not displayed correctly
I am using xamarin forms 5.0.0.2244
var labelCountry = new Label
{
Text = $"Bienvenido a {Settings.Country} ",
FontSize = 14,
TextColor = Color.Blue
};
read the docs on Layout Options
HorizontalOptions = LayoutOptions.FillAndExpand
this will make the Label expand to fill the size of it's parent container
any ideas how to prepare such an element as at the picture ?
I need 5 buttons to be there so i could swipe through them but only 3 one them are visible all the time. I need this to work on android and ios in xamarin forms.
Try this Add ScrollView and set it's orientation to horizontal
ScrollView = new ScrollView
{
Orientation = ScrollOrientation.Horizontal
};
//ScrollView.Scrolled += ScrollView_Scrolled;
ItemsStackLayout = new StackLayout
{
Orientation = StackOrientation.Horizontal,
Padding = new Thickness(0),
Spacing = 0,
HorizontalOptions = LayoutOptions.FillAndExpand
};
ScrollView.Content = ItemsStackLayout;
you can set the width of the elements inside 1/3 of the view width
FYI I use the same in my app
I wanted chat application type user interface in my app & i am targeting android and iOS.
I am using Xamarin.Forms.Editor for reply
Editor _replyEntry = new Editor
{
HorizontalOptions = LayoutOptions.FillAndExpand,
TextColor = Color.Black
}
in this case the editor height remains constant and allows scrolling and editor does not expands
Then i used InvalidateMeasure()
_replyEntry .TextChanged += (sender, e) => { this.InvalidateMeasure(); };
in this case editor expands as when the text requires more space but does not allow scroll inside editor and if user types long message then editor does not allows scroll and text goes behind the keyboard and not visible to user
Is there any way to enable scroll and give max height to edit either in xamarin.forms of by writing custom renderer
Thanks
Here is my code
public class abc : ContentPage
{
public abc()
{
Image attchment = new Image
{
Source = "attachment.png",
HorizontalOptions = LayoutOptions.Start
};
Editor _replyEntry = new Editor
{
HorizontalOptions = LayoutOptions.FillAndExpand,
TextColor = Color.Black,
};
Button _sendButton = new Button
{
Text = "Send",
TextColor = Color.Black,
BackgroundColor = Color.Transparent,
HorizontalOptions = LayoutOptions.End
};
StackLayout replyStack = new StackLayout
{
Orientation = StackOrientation.Horizontal,
Padding = new Thickness(10),
Spacing = 10,
VerticalOptions = LayoutOptions.End,
Children = { attchment, _replyEntry, _sendButton }
};
Content = replyStack;
}
}
It looks like you will have to use a custom renderer to achieve what you are wanting.
There is a post here that has pretty much the same thing with what you are trying to achieve for Android.
In that demo it has an expanding multi-line EditText control (android:singleLine="false"), with only vertical scrollbars (android:scrollbars="vertical"), whilst disabling the horizontal scrollbars (android:scrollHorizontally="false").
You need to ensure the Editor' parent is expanding, then the editor will automatically expand too. If you make an empty contentpage and add an Editor, the is will just expand. If you place it inside a stacklayout, the you need to ensure that the stacklayout is expanding.
I have Frame with Grid in content. When i added contentWiew (with content) to Grid there are two options:
1. If contentWiew have BackgroundColor = "Transparent" then when i click to the contentWiew pressing happen in Frame (contentWiew skiped through it).
2. If contentWiew have BackgroundColor = NotTransparent (Red,Yellow) then when i click to the contentWiew pressing happen in contentWiew ().
Part of the code:
Label mainText = new Label { Text = "TestText", FontSize = 14 };
var contntView = new ContentView () { BackgroundColor = Color.Transparent , HorizontalOptions = LayoutOptions.End, VerticalOptions = LayoutOptions.Start };
contntView.Content = mainText;
mainGrid.Children.Add (contntView,0,1);
I need to do exactly the turnover =) When ContentView is Transparent - catch click. When ContentView is not Transparent - skip click to Frame.
How can I control this process, regardless of BackgroundColor?
You can partially control this process using any View's InputTransparent property. If it's set to true, the view will be transparent to touch events.
I am working on Xamarin forms project and I've used list view in it. I'm trying to add space between two cells of a list. I've used renderer for that and successfully accomplished it in android. I'm using same approach on iOS but not getting any solution. Also my cells have borders of width 1. So it is not possible to embed one more blank view at bottom of cell in iOS. This is how I've provided border of width two list cell in my custom ViewCell renderer.
UITableViewCell cell = base.GetCell (item, reusableCell, tv);
cell.ContentView.Layer.BorderWidth = 1f;
cell.ContentView.Layer.BorderColor = Color.FromHex ("BFE0E0E0").ToCGColor ();
cell.ContentView.Layer.CornerRadius = 5f;
Please suggest me any way through which I can accomplish spacing between two cells of a list.
I am working with collections and not with tables. In the following layout-example you can set vertical and horizontal spacing:
var layout = new UICollectionViewFlowLayout
{
HeaderReferenceSize = new CGSize(0, 0),
FooterReferenceSize = new CGSize(0, 0),
SectionInset = new UIEdgeInsets(0, 0, 0, 0),
ItemSize = new CGSize(cellwidth, cellheight),
ScrollDirection = UICollectionViewScrollDirection.Vertical,
MinimumInteritemSpacing = verticalspacing,
MinimumLineSpacing = horizontalspacing
};
var mycollection = new UICollectionView(CGRect.Empty, layout);;
Then you just need to set the source of your UiCollectionView to your list of items. In this source you can define the layout/design of a single item in your colleciton (just like a UITableViewCell).