在 Xamarin Forms 中,當我將標簽放在具有許多列的網格中時,標簽會占用額外的高度,就像文本更長并在新行下方溢位一樣。
如果我將標簽放在堆疊布局內,在網格之前或之后,它會正確顯示,但在網格內時不會。
此外,如果網格有很多列,問題似乎更大。
如果我洗掉滾動視圖,它不會解決問題。
該問題似乎與堆疊布局的 Margin of 20 集有關。然而,更大的邊距會使事情變得更糟,即使我洗掉了滾動視圖和堆疊布局并且只保留了網格,問題仍然存在。
我正在使用最新的 Xamarin Forms (5.0.0.2291) 和 Xamarin Essentials (1.7.0)。我嘗試了舊版本,例如 XF 4.7.1239 - 問題仍然出現。
<?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:d="http://xamarin.com/schemas/2014/forms/design"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
x:Class="App1.MainPage"
NavigationPage.HasNavigationBar="False"
xmlns:ios="clr-namespace:Xamarin.Forms.PlatformConfiguration.iOSSpecific;assembly=Xamarin.Forms.Core"
ios:Page.UseSafeArea="true">
<ContentPage.Content> <!-- totally not good... -->
<ScrollView Orientation="Vertical">
<StackLayout Orientation="Vertical"
Margin="20"
HorizontalOptions="FillAndExpand"
BackgroundColor="LightGreen">
<Grid Margin="0" Padding="0" BackgroundColor="LightCyan">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="1*" />
<ColumnDefinition Width="1*" />
<ColumnDefinition Width="1*" />
<ColumnDefinition Width="1*" />
<ColumnDefinition Width="1*" />
<ColumnDefinition Width="1*" />
<ColumnDefinition Width="1*" />
<ColumnDefinition Width="1*" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<Label Text="Contact wire height low and high alert limits
contact wire 123456"
FontSize="18"
Padding="0"
Margin="0"
HorizontalOptions="FillAndExpand"
Grid.Row="0"
Grid.Column="0"
Grid.ColumnSpan="8"
BackgroundColor="LightBlue"/>
</Grid>
</StackLayout>
</ScrollView>
</ContentPage.Content>
</ContentPage>
更新

uj5u.com熱心網友回復:
這聽起來像是 Grid 布局邏輯中的一個錯誤,與跨越多列的標簽有關。
您可以通過明確告訴它只使用一行來控制它:
<Label ... MaxLines="1" ... />
或者您可能必須設定明確的行高。例如
<RowDefinition Height="30"/>
解決方法:如果 Grid 有
<Grid ... ColumnSpacing="0" ...>
Padding您可以使用Grid 的元素來近似 ColumnSpacing 所做的事情。不那么方便,但可能。
我在使用自定義標簽類時發現了這一點。第一次OnMeasure呼叫它,widthRequest顯然不包括ColumnSpacing;它不是網格的全寬!
之后,OnMeasure再次以正確的寬度呼叫 - 但我推斷 Grid 已經根據第一次呼叫確定了行高。
uj5u.com熱心網友回復:
- 根據您的描述,Lable 占用了額外的高度。在您的定義中,您使用
<RowDefinition Height="Auto"/>,因此視圖將自動向下擴展。定義一個固定的高度可以使他的身高。正常顯示。 - 你用過
Grid.ColumnSpan="8",使用這段代碼是將8列的空間合二為一,也就是使用一整行的空間,可以看到這個Lable在顯示屏上占據了整行。 - 對于列定義,您使用了 8
1*,這相當于將一行分成 8 個部分,每個部分占用 1/8 的空間。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/415580.html
標籤:
