2017-12-06 5 views
0

저는 Xamarin 양식이 새로운 것입니다 (Swift가 내 장점입니다). 모든 화면 크기에서 내 앱을 작동 시키려면 상대 레이아웃을 사용하고 있습니다. 유일한 문제는 너비와 높이를 설정하는 방법 밖에 찾을 수 없다는 것입니다.Xamarin은 상대적인 레이아웃 제한을 상단과 하단에 형성합니다.

위쪽 및 아래쪽 구속 조건을 설정하고 싶습니다.

내가 신속한에서 할 수있는 것입니다 :

레드 박스 = UIView의하자() redBox.translatesAutoresizingMaskIntoConstraints = 거짓 redBox.backgroundColor =이 다음 화면에 결과

let blueBox = UIView() 
    blueBox.translatesAutoresizingMaskIntoConstraints = false 
    blueBox.backgroundColor = .blue 

    view.addSubview(redBox) 
    view.addSubview(blueBox) 

    redBox.widthAnchor.constraint(equalTo: view.widthAnchor).isActive = true 
    redBox.topAnchor.constraint(equalTo: view.topAnchor).isActive = true 
    redBox.heightAnchor.constraint(equalToConstant: 150).isActive = true 

    blueBox.widthAnchor.constraint(equalTo: view.widthAnchor).isActive = true 
    blueBox.topAnchor.constraint(equalTo: redBox.bottomAnchor).isActive = true 
    blueBox.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true 

.red :

enter image description here

나는 생을 달성 할 방법 Xamarin 형태로? 너비 및 높이 제한을 수행하는 방법 만 알 수 있습니다. 또한 XAML 또는 끌어서 놓기 대신 프로그래밍 방식으로 수행하려고합니다. 여기

는 C# 자 마린 형태 코드 내가 발견 한되는 :

var layout = new RelativeLayout(); 
      Content = layout; 

      var aquaBox = new BoxView() 
      { 
       Color = Color.Aqua 
      }; 

      var silverBox = new BoxView 
      { 
       Color = Color.Silver 
      }; 

      aquaBox.HeightRequest = 150; 
      layout.Children.Add(aquaBox, 
       widthConstraint: Constraint.RelativeToParent(parent => parent.Width) 
      ); 

      layout.Children.Add(silverBox, 
       yConstraint: Constraint.RelativeToView(aquaBox, (RelativeLayout, element) => element.Height) 
      ); 

XAML :

<?xml version="1.0" encoding="utf-8"?> 
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" xmlns:local="clr-namespace:Test" x:Class="Test.TestPage"> 

</ContentPage> 

이 코드의 결과는 다음과 같습니다

enter image description here

무엇 C# Xamarin 폼 코드는 신속하게 볼 수있는 동일한 뷰를 얻는 데 필요합니까?

참고 : Xamarin 양식 코드를 사용하고 Android, Windows 및 iOS 용 코드를 분리하지 마십시오.

답변

0

어떻게 상대적 레이아웃으로 구현할지 모르겠지만 그리드에서는 쉽습니다.

var aquaBox = new BoxView 
{ 
    Color = Color.Aqua, 
    HeightRequest = 150 
}; 

var silverBox = new BoxView 
{ 
    Color = Color.Silver 
}; 

var grid = new Grid { RowSpacing = 0 }; 
grid.RowDefinitions.Add(new RowDefinition { Height = GridLength.Auto}); 
grid.RowDefinitions.Add(new RowDefinition { Height = GridLength.Star }); 

grid.Children.Add(aquaBox, 0, 0); 
grid.Children.Add(silverBox, 0, 1); 

Content = grid; 
0

RelativeLayout의 :

가 heightConstraint 매개 변수의 높이를 찾습니다. RelativeLayout의 VerticalOptions은 기본적으로 Fill이며 뷰 (즉, RelativeLayout)의 크기를 조정하면 parent.Height이 다시 계산됩니다.

var layout = new RelativeLayout(); //VerticalOptions is default Fill 

var aquaBox = new BoxView 
{ 
    Color = Color.Aqua, 
    HeightRequest = 150 
}; 

var silverBox = new BoxView 
{ 
    Color = Color.Silver 
}; 

layout.Children.Add(aquaBox, 
    widthConstraint: Constraint.RelativeToParent((parent) => parent.Width) 
    ); 

layout.Children.Add(silverBox, 
    widthConstraint: Constraint.RelativeToParent((parent) => parent.Width), 
    yConstraint: Constraint.RelativeToView(aquaBox, (parent, sibling) => sibling.Height), 
    heightConstraint: Constraint.RelativeToParent((parent) => parent.Height - aquaBox.HeightRequest) 
    ); 

Content = layout;