问题
I have a frame with 2 buttons and label. What is the best practice to make sure the frame and controls inside resize according to the screen size?
Whatever I have tried doesnt seem to do it!!! I thought that flexlayout could do it out of the box ,but cannot make it work.
I have used absolute layout to resize the frame.
Any suggestions
<AbsoluteLayout HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand" BackgroundColor="Lavender">
<Frame Margin="20"
AbsoluteLayout.LayoutBounds="0.5,.05,0.9,0.4" AbsoluteLayout.LayoutFlags="All"
HorizontalOptions="FillAndExpand"
VerticalOptions="FillAndExpand"
BackgroundColor="WhiteSmoke"
BorderColor="DarkGray"
CornerRadius="10">
<FlexLayout
Padding="0"
AlignContent="Center"
AlignItems="Center"
Direction="Column"
JustifyContent="Center"
Wrap="NoWrap">
<Label Text="Label1" FontAttributes="Bold" FlexLayout.AlignSelf="Center" />
<Grid FlexLayout.AlignSelf="Center" ColumnSpacing="30">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"></RowDefinition>
<RowDefinition Height="Auto"></RowDefinition>
<RowDefinition Height="Auto"></RowDefinition>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"></ColumnDefinition>
<ColumnDefinition Width="Auto"></ColumnDefinition>
</Grid.ColumnDefinitions>
<Button Grid.Row="0"
Grid.Column="0"
Text="A"
MinimumHeightRequest="50"
MinimumWidthRequest="50"
HeightRequest="50"
WidthRequest="50"></Button>
<Button Grid.Row="0" Grid.Column="1"
Text="B"
MinimumHeightRequest="50"
MinimumWidthRequest="50"
HeightRequest="50"
WidthRequest="50" />
<Label Grid.Row="1" Grid.Column="0" Text="Label2" HorizontalTextAlignment="Center"></Label>
<Label Grid.Row="1" Grid.Column="1" Text="Label3" HorizontalTextAlignment="Center"></Label>
</Grid>
</FlexLayout>
</Frame>
</AbsoluteLayout>
回答1:
You could change the width and height according to your frame via binding Value Converters.
Binding Value Converters: https://docs.microsoft.com/en-us/xamarin/xamarin-forms/app-fundamentals/data-binding/converters
Set the name to your Frame first.
<Frame
…………
x:Name="frame"/>
Create the MyConverter. MyConverter.cs
public class MyConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
return (double)value / 3.0;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
Set the StaticResource.
<ContentPage.Resources>
<ResourceDictionary>
<local:MyConverter x:Key="MyConverter" />
</ResourceDictionary>
</ContentPage.Resources>
Binding to your button.
<Button Grid.Row="0" Grid.Column="0" Text="A"
WidthRequest="{Binding Source={x:Reference frame},Path=Width,Converter={StaticResource MyConverter}}"
HeightRequest="{Binding Source={x:Reference frame},Path=Height,Converter={StaticResource MyConverter}}"></Button>
<Button Grid.Row="0" Grid.Column="1" Text="B"
WidthRequest="{Binding Source={x:Reference frame},Path=Width,Converter={StaticResource MyConverter}}"
HeightRequest="{Binding Source={x:Reference frame},Path=Height,Converter={StaticResource MyConverter}}"/>
来源:https://stackoverflow.com/questions/58987878/resizing-frame-and-controls-according-to-device-size-suggestions