我正在開發一個 Xamarin Android 專案,并有一個CollectionView帶有一些動態Button專案(目前為 6 個),但它們似乎被剪掉或“超出了框框”(我希望你理解 - 我不知道確切的描述這)。
示例代碼(簡化):
<CollectionView
ItemsLayout="HorizontalList"
ItemsSource="{Binding Items}">
<CollectionView.ItemTemplate>
<DataTemplate>
<Button Text="Text" />
</DataTemplate>
</CollectionView.ItemTemplate>
</CollectionView>
結果截圖:

請注意,影像中僅顯示了 4? 個專案 - 其余專案被“切斷”或“剪掉”,即在外面(或任何你稱之為的專案)并且不顯示。
如果我在網格中手動插入專案(并且不使用系結),我可以獲得我想要的結果。
請參閱此示例代碼:
<Grid ColumnSpacing="8">
<Button Grid.Column="0" Text="Text" />
<Button Grid.Column="1" Text="Text" />
<Button Grid.Column="2" Text="Text" />
<Button Grid.Column="3" Text="Text" />
<Button Grid.Column="4" Text="Text" />
<Button Grid.Column="5" Text="Text" />
</Grid>
結果截圖:

那么如何確保 a 中的可系結專案CollectionView非常均勻地適合,即沒有被剪掉(并且具有相同的自動寬度)?
太感謝了!
uj5u.com熱心網友回復:
謝謝您的意見。
我能夠找到另一個可行的解決方案,即如何使用FlexLayout控制元件作為我的容器來很好地適應我的專案。
代碼示例:
<StackLayout>
<FlexLayout BindableLayout.ItemsSource="{Binding Items}"
JustifyContent="SpaceBetween"
Wrap="NoWrap">
<BindableLayout.ItemTemplate>
<DataTemplate x:DataType="{x:Null}">
<Button FlexLayout.Basis="14%" Text="{Binding Text}" />
</DataTemplate>
</BindableLayout.ItemTemplate>
</FlexLayout>
</StackLayout>
uj5u.com熱心網友回復:
是的,CollectionView是使用不同布局規范呈現資料串列的視圖。它旨在為ListView.
并且CollectionView應該用于呈現需要滾動或選擇的資料串列。就像 Andrew,在您的代碼中我們可以發現 property 的ItemsLayout值為HorizontalList,因此如果內容不能完全水平顯示,CollectionView 將水平滾動。
ItemsLayout="HorizontalList"
從檔案Xamarin.Forms CollectionView Layout中,我們知道
默認情況下,CollectionView 將在垂直串列中顯示其專案。但是,可以使用以下任何布局:
垂直串列
——隨著新 專案的添加而垂直增長的單列串列。水平串列
——隨著新 專案的添加而水平增長的單行串列。垂直網格——一個多列網格,隨著新
專案的添加而垂直增長。水平網格——一個多行網格,隨著新
專案的添加而水平增長。
可以通過將 ItemsLayout 屬性設定為派生自 ItemsLayout 類的類來指定這些布局。此類定義以下屬性:
- Orientation型別為 ItemsLayoutOrientation ,指定添加專案時 CollectionView 展開的方向。
- SnapPointsAlignment型別的 SnapPointsAlignment 指定捕捉點如何與專案對齊。
- SnapPointsType型別為 SnapPointsType,指定滾動時捕捉點的行為。
更多內容可以查看官方檔案CollectionView Introduction 和CollectionView Layout。
另外,如果你不想滾動,當要顯示的資料不需要滾動或選擇時,可以使用可系結布局。有關詳細資訊,請參閱Xamarin.Forms 中的可系結布局。
請參考以下代碼:
<StackLayout BindableLayout.ItemsSource="{Binding Items}"
Orientation="Horizontal" >
<BindableLayout.ItemTemplate>
<DataTemplate>
<Button Text="Text" WidthRequest="50" />
</DataTemplate>
</BindableLayout.ItemTemplate>
</StackLayout>
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/425089.html
