2017-08-14 122 views
0

我必須設置一個問題爲Xamarin適當佈局窗體定位在Xamarin形式與中繼器,相對圖和滾動

我需要具有其中按鈕由中繼器所生成的佈局。該區域必須是可滾動的,以便在Repeater/3的高度處只有3個按鈕可見,並且它們填滿整個區域,其餘部分將滾動。所有按鈕都需要有相同的高度(Repeater/3)。我不知道按鈕的數量

我有這樣的事情

 <ScrollView HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand" Grid.Row="3" Grid.RowSpan="2" Grid.Column="1" BackgroundColor="Red"> 
     <RelativeLayout x:Name="XName"> 
      <controls:RepeaterView ItemsSource="{Binding Buttons}" x:Name="Test" 
      RelativeLayout.WidthConstraint="{ConstraintExpression Type=RelativeToParent,Property=Width,Factor=1,Constant=0}" 
      RelativeLayout.HeightConstraint="{ConstraintExpression Type=RelativeToParent,Property=Height,Factor=1,Constant=0}"> 
       <controls:RepeaterView.ItemTemplate> 
        <DataTemplate> 
         <ViewCell> 
          <RelativeLayout 
           HorizontalOptions="FillAndExpand" 
           RelativeLayout.HeightConstraint="{ConstraintExpression Type=RelativeToView,ElementName=Test Property=Height,Factor=1,Constant=0}"> 
           <Button Text="{Binding Name}" RelativeLayout.WidthConstraint="{ConstraintExpression Type=RelativeToParent,Property=Width,Factor=1,Constant=0}"/> 
          </RelativeLayout> 
         </ViewCell> 
        </DataTemplate> 
       </controls:RepeaterView.ItemTemplate> 
      </controls:RepeaterView> 
     </RelativeLayout> 
    </ScrollView> 

我的代碼會產生某事像這樣:

enter image description here

我需要的是這樣的:

enter image description here

圖像的內容並不重要。我需要按鈕縮放,以便3個按鈕將所有紅色區域,其餘(相同大小)將被圈起

回答

0

不要讓ScrollView「FillAndExpand」垂直。相反,給它HeightRequest的3個按鈕+填充或將ScrollView放在3個按鈕高的小容器中。你

也可以把它放在Grid這樣的:

<Grid> 
    <Grid.RowDefinitions> 
     <RowDefinition Height="3*" /> 
     <RowDefinition Height="7*" /> 
    </Grid.RowDefinitions> 
    <ScrollView HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand" Grid.Row="0" BackgroundColor="Red"> 
     <controls:RepeaterView ItemsSource="{Binding Buttons}" x:Name="Test"> 
      <controls:RepeaterView.ItemTemplate> 
       <DataTemplate> 
        <ViewCell> 
         <RelativeLayout 
          HorizontalOptions="FillAndExpand" 
          RelativeLayout.HeightConstraint="{ConstraintExpression Type=RelativeToView,ElementName=Test Property=Height,Factor=1,Constant=0}"> 
          <Button Text="{Binding Name}" RelativeLayout.WidthConstraint="{ConstraintExpression Type=RelativeToParent,Property=Width,Factor=1,Constant=0}"/> 
         </RelativeLayout> 
        </ViewCell> 
       </DataTemplate> 
      </controls:RepeaterView.ItemTemplate> 
     </controls:RepeaterView> 
    </ScrollView> 
</Grid> 

所以這第一排將填補3/10屏幕和第二排的(0.3或30%)將填滿整個屏幕的7/10。

+0

謝謝你這個答案。 3個按鈕的高度意味着填充整個紅色區域(我希望按鈕在我的照片中更高)。我不想使用任何絕對尺寸,並保持可擴展性。 Scroolview已經位於一個網格區域,覆蓋3個按鈕的所需高度。問題是中繼器以不同的方式產生它,並在該區域放置5-6個按鈕。 – user1658223

+0

把網格放在哪裏?它應該被視爲重複項目還是作爲容器?對不起,這些愚蠢的問題,但我是Xamarin的新手,它真的讓我惱火;) – user1658223

+0

你的代碼在這種情況下只會將整個紅色區域的高度降低到1/3。我需要一個按鈕來使紅色區域的面積爲1/3,但整個紅色區域必須保持不變 - 必須在那裏放置3個按鈕。 – user1658223

0

確定我已成功地部分做什麼,我打算:

  <ScrollView Orientation="Vertical" HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand"> 
      <RelativeLayout HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand" BackgroundColor="Green"> 
       <controls:RepeaterView ItemsSource="{Binding Buttons}" x:Name="Test" 
       VerticalOptions="FillAndExpand" 
       RelativeLayout.HeightConstraint="{ConstraintExpression Type=RelativeToParent,Property=Height,Factor=2}" 
       RelativeLayout.WidthConstraint="{ConstraintExpression Type=RelativeToParent,Property=Width,Factor=1}"> 
        <controls:RepeaterView.ItemTemplate> 
         <DataTemplate> 
          <ViewCell> 
           <Button Text="{Binding Name}" HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand"/> 
          </ViewCell> 
         </DataTemplate> 
        </controls:RepeaterView.ItemTemplate> 
       </controls:RepeaterView> 
      </RelativeLayout> 

的問題是,滾動視圖不顯示所有elementst,但只有3

+0

問題是,滾動視圖不顯示所有元素,但只有3.好。這正是你所要求的。 –