Xamarin. Макеты - layouts

Это видео - часть бесплатного обучения в Xamarin University. Чтобы просмотреть весь курс, посетите https://university.xamarin.com/.
Виды макетов
StackLayout
Вертикальная разметка
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var layout = new StackLayour { | |
Orientation = StackOrientation.Vertical | |
}; | |
layout.Children.Add(new Label { Text = "Enter your name:"}); | |
layout.Children.Add(new Entry()); | |
layout.Children.Add(new Button { Text = "ОК"} ); |
Горизонтальная разметка
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
new StackLayout{ | |
Spacing = 0, | |
Orientation = StackOrientation.Horizontal, | |
Children = { | |
new Label { | |
HorizontalOpions = LayoutOptions.CenterAndExpand | |
}, | |
new Label { | |
Text = "horizontal.", | |
}, | |
} | |
} |
AbsoluteLayout
Дочерние элементы позиционируются относительно левого верхнего угла экрана.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var layout = new AbsoluteLayout(); | |
... | |
//Ставит абсолютную позицию по координатной точке | |
layout.Children.Add(label1, new Point(100, 100)); | |
//Или использует конкретую ограничительную рамку | |
layout.Children.Add(label2, new Rectangle(20, 20, 100, 25)); |
RelativeLayout
Использует зависимость от значений родительского компонента.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<BoxView Color="Green" WidthRequest="50" HeightRequest="50"> | |
RelativeLayout.XConstraint = | |
"{ConstraintExpression Type=RelativeToParent, | |
Property=Width, | |
Factor=0.5, | |
Constant = -100}" | |
RelativeLayout.YConstraint = | |
"{ConstraintExpression Type=RelativeToParent, | |
Property=Height, | |
Factor=0.5, | |
Constant = -100}" |
Grid
Представляет собой табличный макет.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Label label = new Label { Text = "Enter Your Name" }; | |
Grid layout = new Grid(); | |
layout.Children.Add(label); | |
//Используем статические методы Grid для установки позиции в таблице(сетке) | |
Grid.SetColumn(label, 1); | |
Grid.SetRow(label, 1); | |
Grid.SetColumnSpan(label, 2); | |
Grid.SetRowSpan(label, 1); |
ScrollView
Представляет собой прокручиваемый контейнер.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var stack = new StackLayout(); | |
var label = new Label {...}; | |
var button = new Button {...}; | |
var box = new BoxView {...}; | |
stack.AddChildren.Add(label); | |
stack.AddChildren.Add(button); | |
stack.AddChildren.Add(box); | |
... | |
var scroll = new ScrollView { Content = stack }; |
Длина и ширина
Свойства Height и Width вернут высоту и ширину соответственно. Они могут быть 0 до тех пор, пока вид не будет размещен и не отображен на экране. Помните, что высота и ширина не могут быть установлены напрямую, для этого используем HeightRequest и WidthRequest соответственно. Непосредственные длина и ширина определяются свойствами WidthRequest и HeightRequest соответственно.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var Button = new Button(); | |
button.WidthRequest = 300; | |
button.HeightRequest = 150; |
Отступ c внешней стороны
Отступ c внешней стороны определяется свойством Margin.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Label label = ... | |
Button button = ... | |
label.Marin = new Thickness(10); | |
button.Margin = new new Thickness(10, 20); |
Отступ c внутренней стороны
Отступ c внутренней стороны контейнера определяется свойством Padding.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Grid grid = ...; | |
grid.Padding = new Thickness(10); |
Отступ между элементами в контейнере
Отступ между элементами в контейнере StackLayout определяется свойством Spacing.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
StackLayout panel = ...; | |
panel.Spacing = 20; //отступ между элементами со всех сторон |
Комментарии
Отправить комментарий