5
Xamarin.Forms에서 Height
및 Width
컨트롤의 속성을 사용하려고하면 둘 다 -1을 반환하고 상대 레이아웃이 화면에서 벗어나게 표시됩니다.Xamarin.Forms : 상대 레이아웃을 사용하여 뷰를 중앙에 배치하는 방법? `Width`와`Height`는 -1을 반환합니다.
var mainLayout = new RelativeLayout();
//Add the Switch to the center of the screen
mainLayout.Children.Add(mySwitch,
Constraint.RelativeToParent(parent => parent.Width/2 - mySwitch.Width/2),
Constraint.RelativeToParent(parent => parent.Height/2 - mySwitch.Height/2));
//Add a Label below the switch
mainLayout.Children.Add(switchLabel,
Constraint.RelativeToParent(parent => parent.Width/2 - switchLabel.Width/2),
Constraint.RelativeToView(mySwitch, (parent, view) => view.Y + mySwitch.Height + 10));
Content = mainLayout;
동적 텍스트가 포함 된 라벨을 가운데 맞춤하기에 좋습니다. 텍스트의 크기가 증가하거나 감소함에 따라 상대적 레이아웃은 자동으로 너비를 다시 계산하고 레이블을 중앙에 유지합니다! –
Xamarin Forms의 CaptainXamtastic의 답은 다음을 설명하는 훌륭한 작업입니다. https://forums.xamarin.com/discussion/22902/how-to-add-a-label-to-a-relative-layout-and -center-it-horizontally –
XAML에서이 작업을 수행 할 수있는 방법이 있습니까? –